前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >laravel用crud之index列出产品items

laravel用crud之index列出产品items

作者头像
ytkah
发布2018-08-01 11:02:19
4040
发布2018-08-01 11:02:19
举报
文章被收录于专栏:ytkahytkah

  前面我们说了laravel用crud修改产品items-新建resource controller和routing,现在我们要把产品items罗列出来,需要修改路由和模板,一起随ytakh来看看把

  1,修改controller,/app/Http/Controllers/ItemController.php

代码语言:javascript
复制
use App\Item;
//还有下面的index定义
    public function index()
    {
        //
      $items = Item::all();
        return view('items.index')->with('items',$items); 
    }

  2,修改index.blade.php模板

代码语言:javascript
复制
@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

代码语言:javascript
复制
use App\Item;
use DB;
//还有下面的function定义
    public function index()
    {
        //
        $items = DB::table('items')->paginate(10);//可以调整数字大小,表示一页显示多少各产品
        return view('items.index')->with('items',$items); 
    }

如果要降序排列,即最新上传的产品放在前面,用 ->latest()

代码语言:javascript
复制
$items = DB::table('items')->latest()->paginate(1);

  修改index.blade.php模板

代码语言:javascript
复制
@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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档