在本篇文章中,我将给大家分享laravel 5.6版本中的基本crud(创建,读取,更新和删除)应用程序模块。你可以按照下面的步骤在laravel 5.6中创建crud应用程序。
laravel是一个流行的开源php mvc框架,具有许多高级开发功能。如果你是laravel 5.6应用程序中的学习者或初学者,更多地了解或学习crud应用程序总是有很大帮助的。(相关laravel视频教程:《最新laravel商城实战视频教程》)
下面我将创建insert(插入)、update(更新)、delete(删除)和view(查看)和产品的分页示例。你只需创建新产品,查看产品,编辑产品并从列表中删除产品即可。
第1步:安装laravel 5.6
可以在终端中运行 create-project 命令来安装 laravel:
composer create-project --prefer-dist laravel/laravel blog
(相关推荐:《怎么通过composer安装laravel框架?》)
第2步:数据库配置
完成安装后,我们将为laravel 5.6的crud应用程序进行数据库配置,例如数据库名称,用户名,密码等。所以,让我们打开.env文件并填写相关信息,如下:
.env
db_connection=mysqldb_host=127.0.0.1db_port=3306db_database=here your database name(blog)db_username=here database username(root)db_password=here database password(root)
第3步:创建产品表和模型
我们将为产品创建crud应用程序。所以我们必须使用laravel 5.6 php artisan命令创建产品表的迁移(migrations),首先使用以下命令:
php artisan make:migration create_products_table --create=products
在执行此命令之后,你可以在路径database/migrations中找到一个文件,并且必须将以下代码放在migrations文件中以用于创建products表。
<?phpuse illuminate\support\facades\schema;use illuminate\database\schema\blueprint;use illuminate\database\migrations\migration;class createproductstable extends migration{ /** * run the migrations. * * @return void */ public function up() { schema::create('products', function (blueprint $table) { $table->increments('id'); $table->string('name'); $table->text('detail'); $table->timestamps(); }); } /** * reverse the migrations. * * @return void */ public function down() { schema::dropifexists('products'); }}
第4步:添加resource路由
在这个步骤中,我们需要为产品crud应用添加resource路由。所以打开routes / web.php文件并添加以下路由。
routes/web.php
route::resource('products','productcontroller');
第5步:创建productcontroller
现在,我们应该创建一个新的控制器productcontroller。因此要运行以下命令并创建新的控制器。下面的控制器用于创建resource控制器。
创建productcontroller
php artisan make:controller productcontroller --resource --model=product
在下面的命令之后,你将在这个路径app/http/controllers/productcontroller.php中找到新的文件。
在这个控制器中,默认情况下将创建7个方法如下所示:
1)index()
2)create()
3)store()
4)show()
5)edit()
6)update()
7)destroy()
因此,让我们复制下面的代码并将其放到productcontroller.php文件中。
app/http/controllers/productcontroller.php
<?phpnamespace app\http\controllers;use app\product;use illuminate\http\request;class productcontroller extends controller{ /** * display a listing of the resource. * * @return \illuminate\http\response */ public function index() { $products = product::latest()->paginate(5); return view('products.index',compact('products')) ->with('i', (request()->input('page', 1) - 1) * 5); } /** * show the form for creating a new resource. * * @return \illuminate\http\response */ public function create() { return view('products.create'); } /** * store a newly created resource in storage. * * @param \illuminate\http\request $request * @return \illuminate\http\response */ public function store(request $request) { request()->validate([ 'name' => 'required', 'detail' => 'required', ]); product::create($request->all()); return redirect()->route('products.index') ->with('success','product created successfully.'); } /** * display the specified resource. * * @param \app\product $product * @return \illuminate\http\response */ public function show(product $product) { return view('products.show',compact('product')); } /** * show the form for editing the specified resource. * * @param \app\product $product * @return \illuminate\http\response */ public function edit(product $product) { return view('products.edit',compact('product')); } /** * update the specified resource in storage. * * @param \illuminate\http\request $request * @param \app\product $product * @return \illuminate\http\response */ public function update(request $request, product $product) { request()->validate([ 'name' => 'required', 'detail' => 'required', ]); $product->update($request->all()); return redirect()->route('products.index') ->with('success','product updated successfully'); } /** * remove the specified resource from storage. * * @param \app\product $product * @return \illuminate\http\response */ public function destroy(product $product) { $product->delete(); return redirect()->route('products.index') ->with('success','product deleted successfully'); }}
ok,运行下面命令后,你会找到app/product.php,并将下面的内容放入product.php文件中:
app/product.php
<?phpnamespace app;use illuminate\database\eloquent\model;class product extends model{ /** * the attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'detail' ];}
第6步:创建blade文件
现在我们进入最后一步。在这一步中,我们只需要创建blade文件。所以我们主要需要创建布局文件,然后创建新的文件夹“products”,然后创建crud app的blade文件。最后需要创建以下blade文件:
1) layout.blade.php
2) index.blade.php
3) show.blade.php
4) form.blade.php
5) create.blade.php
6) edit.blade.php
让我们创建下面的文件,并放入下面的代码。
resources/views/products/layout.blade.php
<!doctype html><html><head> <title>laravel 5.6 crud application</title> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"></head><body><div class="container"> @yield('content')</div></body></html>
resources/views/products/index.blade.php
@extends('products.layout')@section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>laravel 5.6 crud example from scratch</h2> </div> <div class="pull-right"> <a class="btn btn-success" href="{{ route('products.create') }}"> create new product</a> </div> </div> </div> @if ($message = session::get('success')) <div class="alert alert-success"> <p>{{ $message }}</p> </div> @endif <table class="table table-bordered"> <tr> <th>no</th> <th>name</th> <th>details</th> <th width="280px">action</th> </tr> @foreach ($products as $product) <tr> <td>{{ ++$i }}</td> <td>{{ $product->name }}</td> <td>{{ $product->detail }}</td> <td> <form action="{{ route('products.destroy',$product->id) }}" method="post"> <a class="btn btn-info" href="{{ route('products.show',$product->id) }}">show</a> <a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">edit</a> @csrf @method('delete') <button type="submit" class="btn btn-danger">delete</button> </form> </td> </tr> @endforeach </table> {!! $products->links() !!}@endsection
resources/views/products/show.blade.php
@extends('products.layout')@section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2> show product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> back</a> </div> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>name:</strong> {{ $product->name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>details:</strong> {{ $product->detail }} </div> </div> </div>@endsection
resources/views/products/create.blade.php
@extends('products.layout')@section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>add new product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> back</a> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> <strong>whoops!</strong> there were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('products.store') }}" method="post"> @csrf <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>name:</strong> <input type="text" name="name" class="form-control" placeholder="name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>detail:</strong> <textarea class="form-control" style="height:150px" name="detail" placeholder="detail"></textarea> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">submit</button> </div> </div> </form>@endsection
resources/views/products/edit.blade.php
@extends('products.layout')@section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>edit product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> back</a> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> <strong>whoops!</strong> there were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('products.update',$product->id) }}" method="post"> @csrf @method('put') <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>name:</strong> <input type="text" name="name" value="{{ $product->name }}" class="form-control" placeholder="name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>detail:</strong> <textarea class="form-control" style="height:150px" name="detail" placeholder="detail">{{ $product->detail }}</textarea> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">submit</button> </div> </div> </form>@endsection
现在,我们准备运行我们的crud应用程序的例子,所以运行以下命令快速运行:
php artisan serve
最后你就可以在浏览器上打开下面的网址进行查看测试:
http://localhost:8000/products
本篇文章就是关于laravel 5.6中的curd操作即创建,读取,更新和删除操作,希望对需要的朋友有所帮助!
以上就是laravel 5.6中的curd操作(代码示例详解)的详细内容。