前面我们说了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. 追踪go语言(golang)的新版本新特性【摘抄】

    Go 2.0 新特性展望:详细 go2.0 会有什么新特性呢?下图是一个老外的调侃,他不希望发生这样的事情(please don't make it happen).我倒是希望其中一些实现,比如泛型和 ...

  2. 完全卸载MySQL

    1.先停止mysql服务,cmd模式下输入net stop mysql; 或者在图形服务面板停止mysql服务 2.进入控制面板卸载mysql; 3.windows+R运行“regedit”文件,打开 ...

  3. laravel 在linux环境下解决.htaccess无效和去除index.php

    LoadModule rewrite_module modules/mod_rewrite.so (去掉前面的#注释) AllowOverride All (根目录的配置下,确保设置成All) < ...

  4. [Python] 06 - Modules --> Packages

    故事背景 一.阶级关系 1. Programs are composed of modules.2. Modules contain statements.3. Statements contain ...

  5. SpringBoot------Servlet3.0的注解自定义原生Listener监听器

    前言 常用监听器: //contextListener可以监听数据库的连接,第三方组件的交互,还有静态文件加载等等 servletContextListener HttpSessionListener ...

  6. java调用删除文件的方法删除文件,却删除不干净

    场景: 程序中在做数据下载时,生成了一个临时文件夹.夹子里面有一些txt和其他格式文件. 数据下载完毕后,需要删除这个临时文件夹,但是一直删除不干净,总会有一下文件残留. 网搜到了这个问题的原因: 内 ...

  7. SpringBoot Logback配置,SpringBoot日志配置

    SpringBoot Logback配置,SpringBoot日志配置  SpringBoot springProfile属性配置 ================================ © ...

  8. N76E003 工程创建教程

    一.准备工作: 1.下载编译工具keil c51 2.下载N76E003提供的板级支持包(BSP),可到nuvoton上下载   二.开发环境搭建 1.安装keil c51,然后和谐...不能随便发链 ...

  9. 使用mui.js实现下拉刷新

    闲聊: 最近因公司项目需求,小颖需要写一些html5页面,方便公司IOS和Android给APP中嵌套使用,其中需要实现拉下刷新功能,其实就是调用了一下mui.js就可以啦嘻嘻,下面跟着小颖一起来看看 ...

  10. mysql格式化小数保留小数点后两位(小数点格式化)

    格式化浮点数的问题,用format(col,2)保留两位小数点,出现一个问题,例如下面的语句,后面我们给出解决方法 SELECT FORMAT(12562.6655,2); 结果:12,562.67 ...