上一篇讲了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的更多相关文章

  1. Laravel删除产品-CRUD之delete(destroy)

    上一篇讲了Laravel编辑产品-CRUD之edit和update,现在我们讲一下删除产品,方法和前面的几篇文章类似,照着ytkah来操作吧 1,controller的function destroy ...

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

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

  3. PyQt(Python+Qt)学习随笔:QAbstractItemView的editTriggers属性以及平台编辑键(platform edit key )

    老猿Python博文目录 老猿Python博客地址 editTriggers属性 editTriggers属性用于确认哪些用户操作行为会触发ItemView中的数据项进入编辑模式. 此属性是由枚举类E ...

  4. Laravel展示产品-CRUD之show

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

  5. Emacs和Ultra Edit列编辑模式

    在emacs中可以使用C-r系列组合键进行区域选择编辑,或者使用emacs自带的cua-mode,然后键入C-ret进行可视化列编辑. 使用Ultra Edit同样可以方便的进入列编辑模式,只需要按下 ...

  6. AngularJS Tabular Data with Edit/Update/Delete

    效果 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', ['ui.bootstrap']); app.control ...

  7. 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 ...

  8. 使用laravel一分钟搭建CURD后台页面

    配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...

  9. 《Play for Java》学习笔记(二)基本的CRUD应用

    注解:  CRUD——Create,Retrieve, Update, Delete 文件结构

随机推荐

  1. Spark学习笔记——RDD编程

    1.RDD——弹性分布式数据集(Resilient Distributed Dataset) RDD是一个分布式的元素集合,在Spark中,对数据的操作就是创建RDD.转换已有的RDD和调用RDD操作 ...

  2. react实现多行文本超出加省略号

    http://www.css88.com/archives/5206 overflow : hidden; text-overflow: ellipsis; display: -webkit-box; ...

  3. [Laravel] 10 - WEB API : wrapper

    前言 一.常用的解决方案 React 前端 + PHP (Laravel) 后端 Such as "some exposure to WEB API’s and/or RESTful“. 使 ...

  4. ASP.NET MVC 4 - 上传图片到数据库

    这里演示如何在MVC WEB应用程序如何上传图片到数据库以及如何在WEB页面上显示图片.数据库表对应整个Model类,不单图片数据一个字段,我们从数据表的定义开始: CREATE TABLE [dbo ...

  5. Android使用Fragment实现TabHost效果

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

  6. puppt服务资源管理

    1.服务资源的特性     controllable 提供变量控制     enableable   可以启动 停止服务   refreshable  可以重启服务   2.可用参数: ensure ...

  7. Python scipy 计算短时傅里叶变换(Short-time Fourier transforms)

    计算短时傅里叶变换(STFT) scipy.signal.stft(x,fs = 1.0,window ='hann',nperseg = 256,noverlap = None,nfft = Non ...

  8. J - S-Nim

    Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as ...

  9. 在移动端如何用swiper实现导航栏效果

    我们在写移动端的时候会有滑动和点击导航后滑动到目标页面功能:而这个功能如果自己写的话会很麻烦,所以我在这推荐一下swiper这个插件. 其实swiper中的官网中也有这种功能的实现,但是我认为是有点麻 ...

  10. vue 打包的项目当背景图路径错误

    当背景图路径错误时: 在build/utils.js中添加或更改这句话:publicPath: '../../',