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 ...
随机推荐
- 记一次 Spring 事务配置踩坑记
记一次 Spring 事务配置踩坑记 问题描述:(SpringBoot + MyBatisPlus) 业务逻辑伪代码如下.理论上,插入数据 t1 后,xxService.getXxx() 方法的查询条 ...
- Android使用Fragment实现TabHost效果
现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...
- AndroidのInputFillter之按字符过滤长度,一个中文当两个字符
/** * 以Byte数的方式来实现的LengthFilter * @author bvin */ public class OneByteInputFilter implements InputFi ...
- 不可思议的颜色混合模式 mix-blend-mode (转)
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- Unity3D UGUI Shader画一个圆环
Shader "Unlit/NewUnlitShader" { Properties { _MainTex ("Texture", 2D) = "wh ...
- B - Space Ant
The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists trace ...
- 170830、oracle密码过期ORA-28002: 7天之后口令将过期的解决方法
登陆oracle数据库时错误信息提示:“ORA-28002: 7 天之后口令将过期”. 原因:oracle11g中默认在default概要文件中设置了"PASSWORD_LIFE_TIME= ...
- mysql distinct 后面没有括号
当distinct应用到多个字段的时候,其应用的范围是其后面的所有字段,而不只是紧挨着它的一个字段,而且distinct只能放到所有字段的前面 如下语句是错误的: SELECT country, di ...
- 熟悉ROS系统中的话题
描述:这篇教程主要讲解ROS系统中的话题及rostopic和rqt_plot等命令工具: 1. Setup安装1.1 roscore 首先确保roscore已经启动运行,打开一个新的命令终端,输入如下 ...
- CCPC-Wannafly Winter Camp Day4 Div1 - 夺宝奇兵 - [简单思维题]
题目链接:https://zhixincode.com/contest/18/problem/A?problem_id=259 题目描述 wls正在玩一个寻宝游戏. 宝藏一共有 $n$ 种,都藏在一个 ...