目录
1、注册路由
Route::get('admin/index/dbop4', 'Admin\IndexController@dbop4');
2、添加模型类
(1)产生模型
php artisan make:model Models/Stand
(2)模型属性
模型对应到表,注意添加 可写白名单($fillable 属性)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Stand extends Model
{
//
protected $table = "stand"; //必须
protected $primaryKey = "id"; //必须
public $timestamps = false;
protected $fillable = ['id', 'name', 'age', 'avatar']; //允许写入字段,才能够写入数据
}
3、控制器方法
<?php
namespace App\Http\Controllers\Admin;
use bar\baz\source_with_namespace;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use DB;
use App\Models\Post;
use App\Models\Stand;
class IndexController extends Controller
{
// 数据分页
public function dbop4(){
//$data = Stand::all();
$data = Stand::paginate(5); //自动分页
return view('Admin.dbop4', compact('data'));
}
}
4、分页展示的视图
(1)简单的分页展示
1. 注意需要 {{$data->links()}} 和 paginate() 配合使用才可以自动分页;
2. 注意在 laravel 后端中使用时用 ./path,在浏览器使用时使用 /path,要去掉前面的 . 符号,可以使用 ltrim() 函数实现。
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<title>数据表</title>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>头像地址</th>
<th>头像</th>
</tr>
</thead>
<tbody>
@foreach($data as $key => $val)
<tr>
<td>{{$val->id}}</td>
<td>{{$val->name}}</td>
<td>{{$val->age}}</td>
<td>{{$val->avatar}}</td>
<td><img src="{{ltrim($val->avatar, '.')}}" width="80px" alt="头像"></td>
</tr>
@endforeach
</tbody>
</table>
{{$data->links()}} 当前页码:{{$data->currentPage()}}/{{ceil($data->total() / 5)}} <!-- 上下页链接 -->
</body>
</html>
(2)加入 css 的 laravel 分页样式
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<title>数据表</title>
<!-- 百度的 laravel 分页样式 -->
<style type="text/css">
#pull_right{
text-align:center;
}
.pull-right {
/*float: left!important;*/
}
.pagination {
display: inline-block;
padding-left: 0;
margin: 20px 0;
border-radius: 4px;
}
.pagination > li {
display: inline;
}
.pagination > li > a,
.pagination > li > span {
position: relative;
float: left;
padding: 6px 12px;
margin-left: -1px;
line-height: 1.42857143;
color: #428bca;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;
}
.pagination > li:first-child > a,
.pagination > li:first-child > span {
margin-left: 0;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
}
.pagination > li:last-child > a,
.pagination > li:last-child > span {
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}
.pagination > li > a:hover,
.pagination > li > span:hover,
.pagination > li > a:focus,
.pagination > li > span:focus {
color: #2a6496;
background-color: #eee;
border-color: #ddd;
}
.pagination > .active > a,
.pagination > .active > span,
.pagination > .active > a:hover,
.pagination > .active > span:hover,
.pagination > .active > a:focus,
.pagination > .active > span:focus {
z-index: 2;
color: #fff;
cursor: default;
background-color: #428bca;
border-color: #428bca;
}
.pagination > .disabled > span,
.pagination > .disabled > span:hover,
.pagination > .disabled > span:focus,
.pagination > .disabled > a,
.pagination > .disabled > a:hover,
.pagination > .disabled > a:focus {
color: #777;
cursor: not-allowed;
background-color: #fff;
border-color: #ddd;
}
.clear{
clear: both;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>头像地址</th>
<th>头像</th>
</tr>
</thead>
<tbody>
@foreach($data as $key => $val)
<tr>
<td>{{$val->id}}</td>
<td>{{$val->name}}</td>
<td>{{$val->age}}</td>
<td>{{$val->avatar}}</td>
<td><img src="{{ltrim($val->avatar, '.')}}" width="80px" alt="头像"></td>
</tr>
@endforeach
</tbody>
</table>
{{$data->links()}} 当前页码:{{$data->currentPage()}}/{{ceil($data->total() / 5)}} <!-- 上下页链接 -->
</body>
</html>