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 文件结构
随机推荐
- java-信息安全(九)-基于DH,非对称加密,对称加密等理解HTTPS
概述 java-信息安全(七)-基于非对称加密,对称加密等理解HTTPS 如果想要理解好https,请尽量了解好以上信息等. 参看文章: http://www.ruanyifeng.com/blog/ ...
- Spring 3 Java Based Configuration with @Value
Springsource has released the Javaconfig Framework as a core component of Spring 3.0. There is a tre ...
- 验证java引用的小例子
1. 声明一个变量person指向一个引用对象, 然后将这个person添加到集合list中, 然后将变量person指向null, 问:list中添加的person变成null了吗? import ...
- cocos2dx内存管理
cocos2dx基于引用计数管理内存,所有继承自CCObject的对象都将获得引用计数的能力,可通过调用retain成员函数用于引用计数值,调用release减少引用计数值,当计数值减为0时销毁对象. ...
- 解决UEFI启动模式下无法使用U盘启动WIN7安装界面
问题场景 现在很多人都习惯使用U盘进行安装系统,主要是快捷方便.本文主要是讲解一下U盘在UEFI模式下无法启动Windows7安装界面的问题,可能很多人会说使用PE系统进行安装,但是因为我的主板只有独 ...
- CentOS7.5 Linux搭建全文检索--Solr7.4.0单机服务
一.Solr安装环境 1.官方参考文档 Solr教程参考指南:http://lucene.apache.org/solr/guide/7_4/solr-tutorial.html 2.Solr运行环境 ...
- jquery <img> 图片懒加载 和 标签如果没有加载出图片或没有图片,就显示默认的图片
参考链接:http://www.jq22.com/jquery-info390 或压缩包下载地址:链接:http://pan.baidu.com/s/1hsj8ZWw 密码:4a7s 下面是没有 ...
- parent对象
在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...
- 用Python下载美国国家气候数据中心(NCDC)的气候数据
美国国家气候数据中心的官网地址是https://www.ncdc.noaa.gov/ 气候数据的下载地址是: 长格式:ftp://ftp.ncdc.noaa.gov/pub/data/noaa/,这种 ...
- IP地址、MAC地址及端口
概述: IP 是地址,有定位功能(网与网的通讯) (在逻辑上唯一标识一台电脑)(网络层) MAC 是身份证,无定位功能(在子网干活)(在物理上唯一标识一台电脑) (链路层) 首先是如何查看ip ...