Laravel编辑产品-CRUD之edit和update
上一篇讲了Laravel展示产品-CRUD之show,现在我们说一下Laravel编辑产品,涉及到编辑和更新,
1,定义controller,update和create有点相似,我们复制一份过来修改。new item改为item::find
public function edit($id)
{
//
$item = Item::find($id);
return view('items.edit')->with('item', $item); } public function update(Request $request, $id)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'price' => 'required|numeric',
'img' => 'required|max:255',
'description' => 'required|max:255',
]);//检查输入是否合法
$item = Item::find($id); $item->name = $request->name;
$item->price = $request->price;
$item->img = $request->img;
$item->description = $request->description; $item->save();
}
2,编辑edit.blade.php,文件在/resources/views/items/edit.blade.php,添加如下代码,注意method是PUT
@extends('layouts.app')
@if ($errors->any())
<div class="alert alert-danger">
<strong>Errors:</strong>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="card card-default">
<div class="card-header">Edit Item</div>
<div class="card-body">
<form method="POST" action="{{route('items.update', $item->id)}}" aria-label="Register">
@csrf
<input type="hidden" name="_method" value="PUT">
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">Name</label>
<div class="col-md-6">
<input id="name" type="text" name="name" value="{{ $item->name }}" required="required" autofocus="autofocus" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">Price</label>
<div class="col-md-6">
<input id="email" type="text" name="price" value="{{ $item->price }}" required="required" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Img</label>
<div class="col-md-6">
<input id="password" type="text" name="img" class="form-control" value="{{ $item->img }}">
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">Description</label>
<div class="col-md-6">
<input id="password-confirm" type="text" name="description" required="required" class="form-control" value="{{ $item->description }}">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Laravel编辑产品-CRUD之edit和update的更多相关文章
- Laravel删除产品-CRUD之delete(destroy)
上一篇讲了Laravel编辑产品-CRUD之edit和update,现在我们讲一下删除产品,方法和前面的几篇文章类似,照着ytkah来操作吧 1,controller的function destroy ...
- Laravel创建产品-CRUD之Create and Store
上一篇说了laravel用crud之index列出产品items,我们现在试着添加产品,用到CRUD的 Create 和 Store 方法,打开/app/Http/Controllers/ItemCo ...
- PyQt(Python+Qt)学习随笔:QAbstractItemView的editTriggers属性以及平台编辑键(platform edit key )
老猿Python博文目录 老猿Python博客地址 editTriggers属性 editTriggers属性用于确认哪些用户操作行为会触发ItemView中的数据项进入编辑模式. 此属性是由枚举类E ...
- Laravel展示产品-CRUD之show
上一篇讲了Laravel创建产品-CRUD之Create and Store,现在我们来做产品展示模块,用到是show,①首先我们先修改controller,文件是在/app/Http/Control ...
- Emacs和Ultra Edit列编辑模式
在emacs中可以使用C-r系列组合键进行区域选择编辑,或者使用emacs自带的cua-mode,然后键入C-ret进行可视化列编辑. 使用Ultra Edit同样可以方便的进入列编辑模式,只需要按下 ...
- AngularJS Tabular Data with Edit/Update/Delete
效果 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', ['ui.bootstrap']); app.control ...
- how to do a mass update in Laravel5 ( 在Laravel 5里面怎么做大量数据更新 )
Today, I had spent 3 hours to fix one problem, The old program has a bug, originally, when a user pr ...
- 使用laravel一分钟搭建CURD后台页面
配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...
- 《Play for Java》学习笔记(二)基本的CRUD应用
注解: CRUD——Create,Retrieve, Update, Delete 文件结构
随机推荐
- 大数据学习笔记03-HDFS-HDFS组件介绍及Java访问HDFS集群
HDFS组件概述 NameNode 存储数据节点信息及元文件,即:分成了多少数据块,每一个数据块存储在哪一个DataNode中,每一个数据块备份到哪些DataNode中 这个集群有哪些DataNode ...
- classifier in maven
http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html Beside ...
- [Converge] Gradient Descent - Several solvers
solver : {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’}, default: ‘liblinear’ Algorithm to use in the op ...
- 【laravel5.6】 laravel 执行 php artisan route:cache 报错 Unable to prepare route [/] for serialization. Uses Closure.
laravel 在部署的时候.需要优化路由加载,执行命令 php artisan route:cache 报错了.如下 这个异常的错误信息,提示的已经非常明确了:大概意思就是说在闭包里边,是不能够进行 ...
- gitlab图形化使用教程 (mtm推荐)
原文:http://www.restran.net/2016/02/23/git-and-gitlab-guide/?utm_source=tuicool&utm_medium=referra ...
- findpanel 的相关代码
https://blog.csdn.net/zengcong2013/article/details/43118189 In addition to this method, you can use ...
- 【zheng环境准备】安装redis
1.下载源码,解压缩后编译源码 wget http://download.redis.io/release/redis-2.8.3.tar.gz tar -xzf redis-.tar.gz cd r ...
- js函数作用域
函数 1.函数没有用return返回函数时,返回默认参数undefined 结果 return返回得话 就是里面得数值 结果 JS执行过程是上到下,下面的a元素覆盖了上面的a元素 function d ...
- MacTex TexStudio Configuration 配置
在Mac上使用Latex的话主流是安装MacTex,对于IDE的选择有很多,像什么自带的TexShop,或者是TexStudio,Latexian,Texpad,Texmaker等,甚至可以直接使用一 ...
- 洛谷P1141 01迷宫【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1141 题意: 有一个填了0和1的n*n的格子,只能0走到1,1走到0 有m组询问(数据量是1e5),问某一个 ...