laravel用crud之index列出产品items
前面我们说了laravel用crud修改产品items-新建resource controller和routing,现在我们要把产品items罗列出来,需要修改路由和模板,一起随ytakh来看看把
1,修改controller,/app/Http/Controllers/ItemController.php
use App\Item;
//还有下面的index定义
public function index()
{
//
$items = Item::all();
return view('items.index')->with('items',$items);
}
2,修改index.blade.php模板
@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
use App\Item;
use DB;
//还有下面的function定义
public function index()
{
//
$items = DB::table('items')->paginate(10);//可以调整数字大小,表示一页显示多少各产品
return view('items.index')->with('items',$items);
}
如果要降序排列,即最新上传的产品放在前面,用 ->latest()
$items = DB::table('items')->latest()->paginate(1);
修改index.blade.php模板
@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
laravel用crud之index列出产品items的更多相关文章
- laravel用crud修改产品items-新建resource controller和routing
前面我们创建了laravel简单的items产品api,但是需要在数据库添加,如何在网页上直接添加呢?我们可以用view来操作crud(增加Create.读取查询Retrieve.更新Update和删 ...
- Laravel展示产品-CRUD之show
上一篇讲了Laravel创建产品-CRUD之Create and Store,现在我们来做产品展示模块,用到是show,①首先我们先修改controller,文件是在/app/Http/Control ...
- Laravel创建产品-CRUD之Create and Store
上一篇说了laravel用crud之index列出产品items,我们现在试着添加产品,用到CRUD的 Create 和 Store 方法,打开/app/Http/Controllers/ItemCo ...
- Laravel编辑产品-CRUD之edit和update
上一篇讲了Laravel展示产品-CRUD之show,现在我们说一下Laravel编辑产品,涉及到编辑和更新, 1,定义controller,update和create有点相似,我们复制一份过来修改. ...
- Laravel ServiceProvider注册过程及简单使用
Laravel ServiceProvider注册过程及简单使用 还记得facade注册流程吗?回顾下 在bootstrap/app.php中返回$app实例后,通过singleton方法绑定了三个实 ...
- 在window下配置laravel开发环境
1.由于有一点php基础,所以非常想更进一步,就选择据说在国外最流行的php框架来学习了,laravel框架,官网上介绍是为艺术而生,从知乎和一些论坛上看到,laravel学起来并不简单,首先配置问题 ...
- laravel中的$request对象构造及请求生命周期
laravel应用程序中index.php是所有请求的入口.当用户提交一个form或者访问一个网页时,首先由kernel捕捉到该session PHP运行环境下的用户数据, 生成一个request对象 ...
- Memcache 查看列出所有key方法
参考博文: Memcache 查看列出所有key方法 1. cmd上登录memcache telnet 127.0.0.1 11211 2. 列出所有keys stats items // 这条是命 ...
- Laravel的Nginx重写规则完整代码
laravel基本重写规则 location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?$qu ...
随机推荐
- Java8学习笔记(四)--接口增强
增强点 静态方法 public interface InterfacePlus { void run(); static Date createDate(){ return new Date(); } ...
- Windows 8(64位)如何搭建 Android 开发环境与真机测试(转)
可以参考http://wenku.baidu.com/link?url=ghU6IFS1WJXLFKfM_0efv9YQEnMDBrdi9CXwirSs5IOLLeUfdIOh8OOVv0DX89Lt ...
- Excel公式中使用动态计算的地址
例:统计A列第四行开始,到公式所在行的前一行的非空白行的个数: =COUNTA(A4:INDIRECT(ADDRESS(ROW()-,COLUMN())))
- js操作DOM在父元素中的结尾添加子节点注意
impressionHtml=`<img src=${value} alt=""/>`; document.getElementById("wrapper&q ...
- [TensorBoard] Name & Variable scope
TF有两个scope, 一个是name_scope一个是variable_scope 第一个程序: with tf.name_scope("hello") as name_scop ...
- java.util.Stack(栈)的简单使用
import java.util.Stack; import org.junit.Before; import org.junit.Test; /** * Stack(栈)继承了Vector类,底层实 ...
- Excel 函数集(使用过的)
1. SUBTOTAL函数 筛选结果求和 SUBTOTAL(函数编号, 区域) 函数编号 为 1 到 11(包含隐藏值)或 101 到 111(忽略隐藏值)之间的数字,指定使用何种函数在数据清单 ...
- sencha touch 免费培训视频
之前的收费视频现在免费了 sencha touch版本:2.3.1 第一期:https://pan.baidu.com/s/1kUK4OFP 第二期:https://pan.baidu.com/s/1 ...
- day_5.02 py
''' 2018-5-2 18:43:54 设计4s店类 设计模式: 简单工厂模式(通过一个类的分离模式) 讨论耦合性的问题 类与类之间应该是低耦合性 通过有个 初始化 __init__ 来解耦 这样 ...
- ThinkPHP框架 【 AJAX方法返回 】 例子:简单添加一条数据 和 查询一个表里的数据
注:thinkphp使用ajax和之前使用ajax的方法一样,不同点在于之前的ajax中的url指向了一个页面,而thinkphp里面的url需要指向一个操作方法. 在模块控制器Controller文 ...