前面我们说了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的更多相关文章

  1. laravel用crud修改产品items-新建resource controller和routing

    前面我们创建了laravel简单的items产品api,但是需要在数据库添加,如何在网页上直接添加呢?我们可以用view来操作crud(增加Create.读取查询Retrieve.更新Update和删 ...

  2. Laravel展示产品-CRUD之show

    上一篇讲了Laravel创建产品-CRUD之Create and Store,现在我们来做产品展示模块,用到是show,①首先我们先修改controller,文件是在/app/Http/Control ...

  3. Laravel创建产品-CRUD之Create and Store

    上一篇说了laravel用crud之index列出产品items,我们现在试着添加产品,用到CRUD的 Create 和 Store 方法,打开/app/Http/Controllers/ItemCo ...

  4. Laravel编辑产品-CRUD之edit和update

    上一篇讲了Laravel展示产品-CRUD之show,现在我们说一下Laravel编辑产品,涉及到编辑和更新, 1,定义controller,update和create有点相似,我们复制一份过来修改. ...

  5. Laravel ServiceProvider注册过程及简单使用

    Laravel ServiceProvider注册过程及简单使用 还记得facade注册流程吗?回顾下 在bootstrap/app.php中返回$app实例后,通过singleton方法绑定了三个实 ...

  6. 在window下配置laravel开发环境

    1.由于有一点php基础,所以非常想更进一步,就选择据说在国外最流行的php框架来学习了,laravel框架,官网上介绍是为艺术而生,从知乎和一些论坛上看到,laravel学起来并不简单,首先配置问题 ...

  7. laravel中的$request对象构造及请求生命周期

    laravel应用程序中index.php是所有请求的入口.当用户提交一个form或者访问一个网页时,首先由kernel捕捉到该session PHP运行环境下的用户数据, 生成一个request对象 ...

  8. Memcache 查看列出所有key方法

    参考博文: Memcache 查看列出所有key方法 1. cmd上登录memcache telnet 127.0.0.1 11211  2. 列出所有keys stats items // 这条是命 ...

  9. Laravel的Nginx重写规则完整代码

    laravel基本重写规则 location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?$qu ...

随机推荐

  1. 磨刀不误砍柴工——统一日志系统 Log4Net/ExceptionLess

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫必须注明原文地址:www.cnblogs.com/tdws . 一.   写在前面 本文Log4Net介绍了基础的方式,大数据量生产环境不能使用,中等 ...

  2. 【Latex】常用工具包

    字体篇: \usepackage{color} {\color{red}{Hello World!}} 表格篇: 1.resizebox \begin{table*}[!htb] \centering ...

  3. Excel 函数集(使用过的)

    1.   SUBTOTAL函数  筛选结果求和 SUBTOTAL(函数编号, 区域) 函数编号 为 1 到 11(包含隐藏值)或 101 到 111(忽略隐藏值)之间的数字,指定使用何种函数在数据清单 ...

  4. ORA-03135 防火墙超时设置断开db link 连接

    [现象] 应用使用数据库连接池,访问A库时通过dblink查询B库,应用时不时会报错ORA. [过程还原] 当应用获取了一个数据库连接,并在数据库连接中使用了dblink,如果应用到A库的连接不释放, ...

  5. [原]openstack-kilo--issue(二十二) 虚拟机的vnc console图像调用错误

    [问题点] 在打开node compute 上vm的vnc console窗口时候发现vm1-compute1调用的是vm1-controller上的vnc图像 =================== ...

  6. JS正则校验

    /** 用途:检查输入字符串是否为空或者全部都是空格 输入:str:字符串 返回: 如果全是空返回true,否则返回false */ function isNull(str) { if (str == ...

  7. linux-Centos 7下bond与vlan技术的结合

    服务器eno1与eno2作bonding,捆绑成bond0接口,服务器对端交换机端口,同属于301.302号vlan接口 vlan 301: 10.1.2.65/27                  ...

  8. ng之自定义指令

    最近开始研究并使用angular,今天就来简单讲讲对于ng中自定义指令的一下使用心得吧! 相信用过ng的人都对ng中的指令有所了解,指令,我将其理解为AngularJS操作HTML element的一 ...

  9. PHP链接MySQL,查询数据库内容,删除数据库内容。。。记住链接公式!!!

    //扩展类叫MySQLi MySQL是数据库,MySQLi是扩展 Id地址本地网络服务器的地址localhost 如果想链接别人的输入他的服务器id地址. //root代表的是数据库名, //poss ...

  10. 英语专业出身也要走向python

    这两年一直徘徊在学习python和放弃python的道路上不断的徘徊,今年终于没有在蹉跎下去,选择了开始新的自我挑战,零基础开始学习python. 作为一名英语专业毕业的文科生,学习编程还是相对有些困 ...