laravel用crud之index列出产品items
前面我们说了laravel用crud修改产品items-新建resource controller和routing,现在我们要把产品items罗列出来,需要修改路由和模板,一起随ytakh来看看把
1,修改controller,/app/Http/Controllers/ItemController.php
1 2 3 4 5 6 7 8 | use App\Item; // 还有下面的index定义 public function index() { // $items = Item::all(); return view( 'items.index' )->with( 'items' ,$items); } |
2,修改index.blade.php模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | @extends( 'layouts.app' ) @section( 'content' ) <div class= "container" > <div class= "row" > <div class= "col-md-12" > <div class= "panel panel-default" > <div class= "panel-heading" >List of Items< /div > <div class= "panel-body" > <table class= "table" > <thead> < tr > <th> #</th> <th>Name< /th > <th>Price< /th > <th>Img< /th > <th>description< /th > <th>Created At< /th > <th>Update At< /th > <th>Actions< /th > < /tr > < /thead > <tbody> @foreach($items as $item) < tr > <td>{{$item-> id }}< /td > <td>{{$item->name}}< /td > <td>{{$item->price}}< /td > <td>{{$item->img}}< /td > <td>{{$item->description}}< /td > <td>{{$item->created_at}}< /td > <td>{{$item->updated_at}}< /td > <td> <a class= "btn btn-primary" href= "{{route('items.show', '$item->id')}}" >view< /a > <a class= "btn btn-danger" href= "{{route('items.destroy', '$item->id')}}" >delete< /a > < /td > < /tr > @endforeach < /tbody > < /table > <a class= "btn btn-primary" href= "{{route('items.create')}}" >Create New Item< /a > < /div > < /div > < /div > < /div > < /div > @endsection |
上面是用于产品比较少的情况,如果产品多了,我们就要进行分页才好点,怎么做分页呢?用到paginate
1,修改controller,/app/Http/Controllers/ItemController.php
1 2 3 4 5 6 7 8 9 | use App\Item; use DB; // 还有下面的 function 定义 public function index() { // $items = DB::table( 'items' )->paginate(10); // 可以调整数字大小,表示一页显示多少各产品 return view( 'items.index' )->with( 'items' ,$items); } |
如果要降序排列,即最新上传的产品放在前面,用 ->latest()
1 | $items = DB::table( 'items' )->latest()->paginate(1); |
修改index.blade.php模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | @extends( 'layouts.app' ) @section( 'content' ) <div class= "container" > <div class= "row" > <div class= "col-md-12" > <div class= "panel panel-default" > <div class= "panel-heading" >List of Items< /div > <div class= "panel-body" > <table class= "table" > <thead> < tr > <th> #</th> <th>Name< /th > <th>Price< /th > <th>Img< /th > <th>description< /th > <th>Created At< /th > <th>Update At< /th > <th>Actions< /th > < /tr > < /thead > <tbody> @foreach($items as $item) < tr > <td>{{$item-> id }}< /td > <td>{{$item->name}}< /td > <td>{{$item->price}}< /td > <td>{{$item->img}}< /td > <td>{{$item->description}}< /td > <td>{{$item->created_at}}< /td > <td>{{$item->updated_at}}< /td > <td> <a class= "btn btn-primary" href= "{{route('items.show', '$item->id')}}" >view< /a > <a class= "btn btn-danger" href= "{{route('items.destroy', '$item->id')}}" >delete< /a > < /td > < /tr > @endforeach < /tbody > < /table > <div class= "text-center" >{{$items->links()}}< /div > // 分页链接 <a class= "btn btn-primary" href= "{{route('items.create')}}" >Create New Item< /a > < /div > < /div > < /div > < /div > < /div > @endsection |
2,打开试一下http://lawoole.z5w.net/items?page=2

加微信交流
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2015-07-09 微信电脑版也能用公众号自定义菜单 微信1.2 for Windows发布