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

  

原文地址:https://www.cnblogs.com/ytkah/p/9291157.html

时间: 2024-08-13 20:34:13

Laravel编辑产品-CRUD之edit和update的相关文章

Eclipse 插件产品发布成站点形式 Update Site

Eclipse 插件产品发布成站点形式 Update Site 通过Update Site Project项目将自己做的插件产品发布到公网上,给客户或其他测试人员下载和应用,这样自己的插件就以站点的形式暴露给公众了,谁都可以下载下来试用它. 1. 创建Plug-inProject项目 首先我们先按照Eclipse的向导开发一个插件. 之后选择一个HelloWorld模板即可,生成的插件项目代码结构如图所示: 而plugin.xml内容如下: <?xml version="1.0"

laravel关于产品分类的说说说

1.产品分类的数据表结构设计 不管有多少级分类,分类表中只要有以下几个字段即可, id(分类的id号,不管几级分类都有自己一个唯一的id号), name(分类的名称), parentid(分类的上一级id名称,如果为一级分类,则此处值为0), type(这个字段选填,代表当前分类名所属的等级,有这个字段可更快的判断分类名属于几级分类名称) 2.laravel中全部分类的展示: 1).在前端页面最上面加上如下代码 <?phpuse App\Http\Common\PHPTree;?> 2)在Ap

Laravel展示产品-CRUD之show

上一篇讲了Laravel创建产品-CRUD之Create and Store,现在我们来做产品展示模块,用到是show,①首先我们先修改controller,文件是在/app/Http/Controllers/ItemController.php,定义一下show function public function show($id) { $item = Item::find($id); return view('items.show')->with('item', $item); } ②创建sh

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 profile  don't now allow  Multi Logon,  It will update the other login records of [LoginAudit] table of this user, But the code has a problem, it may no

Laravel artisan commands

使用php artisan list 可以看到artisan的所有命令以及选项. 当然你也可以在此基础上扩展自己的命令. 1. key 1.1 key:generate 这是一个加密秘钥,用于保证安全性,在发布程序的时候需要操作这一步骤. 2. generate 2.1 generate:controller 这样就在controllers目录下面添加了一个控制器文件testController.php,该控制符合CRUD(create, receive, update, delete) 该文件

20140625三层架构实现产品的增删改查

产品的增删改查 l  Model: Products.cs     public class Products     {         public System.Guid Id { get; set; }         public System.String Name { get; set; }         public System.String ImagePath { get; set; }         public System.String Msg { get; set

ASP.NET Web API 基本操作(CRUD)

上一篇介绍了ASP.NET Web API的基本知识和原理,这一篇我们通过一个更直观的实例,对产品进行CRUD操作(Create/Read/Update/Delete)来继续了解一下它的基本应用. 创建ASP.NET Web API应用程序  在VS中选择创建一个ASP.NET Web Application应用程序,在向导的下一个窗口中选择Web API模板. 创建Model 这里我们在Models文件夹下创建一个简单的Product model类,用来传递数据. 在Models文件夹上点击右

学习使用MEAN开发RESTful WEB api,实现数据的CRUD

800?"800px":this.width); max-height:800px; height:expression_r(this.height>800?"800px":this.height); } --> MEAN M=MongoDB 非关系型数据库 E=Express Node中的web开发模块 A=Angular.js Google的javascript开发框架 N=Node.js javascript的服务端运行环境 下面通过youtube

DevExpress ASP.NET 使用经验谈(5)-通过ASPxGridView实现CRUD操作

这节,我们将通过使用DevExpress的ASPxGridView控件,实现对数据的CRUD操作. 首先,我们在解决方案中,添加一个网站: 图一 添加新网站 图二 添加DevExpress.Data.v12.2.dll,DevExpress.Xpo.v12.2.dll,以及XPOModel的引用 图三 从工具栏拖放ASPxGridView与XpoDataSource 图四 设置XpoDataSource的类型名称 TypeName,先选择控件,右键-属性(或点击右上角小箭头) 图五 点击Type