当前位置:首页 > CMS教程 > 其它CMS > 列表

Laravel 5.6中的CURD操作(代码示例详解)

发布:smiling 来源: PHP粉丝网  添加日期:2020-01-15 13:26:55 浏览: 评论:0 

在本篇文章中,我将给大家分享laravel 5.6版本中的基本crud(创建,读取,更新和删除)应用程序模块。你可以按照下面的步骤在laravel 5.6中创建CRUD应用程序。

Laravel是一个流行的开源PHP MVC框架,具有许多高级开发功能。如果你是laravel 5.6应用程序中的学习者或初学者,更多地了解或学习crud应用程序总是有很大帮助的。

下面我将创建insert(插入)、update(更新)、delete(删除)和view(查看)和产品的分页示例。你只需创建新产品,查看产品,编辑产品并从列表中删除产品即可。

第1步:安装Laravel 5.6

可以在终端中运行 create-project 命令来安装 Laravel:

composer create-project --prefer-dist laravel/laravel blog

第2步:数据库配置

完成安装后,我们将为laravel 5.6的crud应用程序进行数据库配置,例如数据库名称,用户名,密码等。所以,让我们打开.env文件并填写相关信息,如下:

.env

  1. DB_CONNECTION=mysql 
  2.  
  3. DB_HOST=127.0.0.1 
  4.  
  5. DB_PORT=3306 
  6.  
  7. DB_DATABASE=here your database name(blog) 
  8.  
  9. DB_USERNAME=here database username(root) 
  10.  
  11. 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表。

  1. <?php 
  2.  
  3. use Illuminate\Support\Facades\Schema; 
  4.  
  5. use Illuminate\Database\Schema\Blueprint; 
  6.  
  7. use Illuminate\Database\Migrations\Migration; 
  8.  
  9. class CreateProductsTable extends Migration 
  10.  
  11. { 
  12.  
  13.     /** 
  14.  
  15.      * Run the migrations. 
  16.  
  17.      * 
  18.  
  19.      * @return void 
  20.  
  21.      */ 
  22.  
  23.     public function up() 
  24.  
  25.     { 
  26.  
  27.         Schema::create('products', function (Blueprint $table) { 
  28.  
  29.             $table->increments('id'); 
  30.  
  31.             $table->string('name'); 
  32.  
  33.             $table->text('detail'); 
  34.  
  35.             $table->timestamps(); 
  36.  
  37.         }); 
  38.  
  39.     }
  40.  
  41.     /** 
  42.  
  43.      * Reverse the migrations. 
  44.  
  45.      * 
  46.  
  47.      * @return void 
  48.  
  49.      */ 
  50.  
  51.     public function down() 
  52. //phpfensi.com 
  53.     { 
  54.  
  55.         Schema::dropIfExists('products'); 
  56.  
  57.     } 
  58.  
  59. } 

第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

  1. namespace App\Http\Controllers; 
  2.  
  3. use App\Product; 
  4.  
  5. use Illuminate\Http\Request; 
  6.  
  7. class ProductController extends Controller 
  8.  
  9. { 
  10.  
  11.     /** 
  12.  
  13.      * Display a listing of the resource. 
  14.  
  15.      * 
  16.  
  17.      * @return \Illuminate\Http\Response 
  18.  
  19.      */ 
  20.  
  21.     public function index() 
  22.  
  23.     { 
  24.  
  25.         $products = Product::latest()->paginate(5); 
  26.  
  27.  
  28.  
  29.         return view('products.index',compact('products')) 
  30.  
  31.             ->with('i', (request()->input('page', 1) - 1) * 5); 
  32.  
  33.     } 
  34.  
  35.  
  36.  
  37.     /** 
  38.  
  39.      * Show the form for creating a new resource. 
  40.  
  41.      * 
  42.  
  43.      * @return \Illuminate\Http\Response 
  44.  
  45.      */ 
  46.  
  47.     public function create() 
  48.  
  49.     { 
  50.  
  51.         return view('products.create'); 
  52.  
  53.     } 
  54.  
  55.  
  56.  
  57.     /** 
  58.  
  59.      * Store a newly created resource in storage. 
  60.  
  61.      * 
  62.  
  63.      * @param  \Illuminate\Http\Request  $request 
  64.  
  65.      * @return \Illuminate\Http\Response 
  66.  
  67.      */ 
  68.  
  69.     public function store(Request $request) 
  70.  
  71.     { 
  72.  
  73.         request()->validate([ 
  74.  
  75.             'name' => 'required', 
  76.  
  77.             'detail' => 'required', 
  78.  
  79.         ]); 
  80.  
  81.  
  82.  
  83.         Product::create($request->all()); 
  84.  
  85.  
  86.  
  87.         return redirect()->route('products.index') 
  88.  
  89.                         ->with('success','Product created successfully.'); 
  90.  
  91.     } 
  92.  
  93.  
  94.  
  95.     /** 
  96.  
  97.      * Display the specified resource. 
  98.  
  99.      * 
  100.  
  101.      * @param  \App\Product  $product 
  102.  
  103.      * @return \Illuminate\Http\Response 
  104.  
  105.      */ 
  106.  
  107.     public function show(Product $product) 
  108.  
  109.     { 
  110.  
  111.         return view('products.show',compact('product')); 
  112.  
  113.     } 
  114.  
  115.  
  116.  
  117.     /** 
  118.  
  119.      * Show the form for editing the specified resource. 
  120.  
  121.      * 
  122.  
  123.      * @param  \App\Product  $product 
  124.  
  125.      * @return \Illuminate\Http\Response 
  126.  
  127.      */ 
  128.  
  129.     public function edit(Product $product) 
  130.  
  131.     { 
  132.  
  133.         return view('products.edit',compact('product')); 
  134.  
  135.     } 
  136.  
  137.  
  138.  
  139.     /** 
  140.  
  141.      * Update the specified resource in storage. 
  142.  
  143.      * 
  144.  
  145.      * @param  \Illuminate\Http\Request  $request 
  146.  
  147.      * @param  \App\Product  $product 
  148.  
  149.      * @return \Illuminate\Http\Response 
  150.  
  151.      */ 
  152.  
  153.     public function update(Request $request, Product $product) 
  154.  
  155.     { 
  156.  
  157.          request()->validate([ 
  158.  
  159.             'name' => 'required', 
  160.  
  161.             'detail' => 'required', 
  162.  
  163.         ]); 
  164.  
  165.  
  166.  
  167.         $product->update($request->all()); 
  168.  
  169.  
  170.  
  171.         return redirect()->route('products.index') 
  172.  
  173.                         ->with('success','Product updated successfully'); 
  174.  
  175.     } 
  176.  
  177.  
  178.  
  179.     /** 
  180.  
  181.      * Remove the specified resource from storage. 
  182.  
  183.      * 
  184.  
  185.      * @param  \App\Product  $product 
  186.  
  187.      * @return \Illuminate\Http\Response 
  188.  
  189.      */ 
  190.  
  191.     public function destroy(Product $product) 
  192.  
  193.     { 
  194. //phpfensi.com 
  195.         $product->delete(); 
  196.  
  197.  
  198.  
  199.         return redirect()->route('products.index') 
  200.  
  201.                         ->with('success','Product deleted successfully'); 
  202.  
  203.     } 
  204.  
  205. } 

OK,运行下面命令后,你会找到app/Product.php,并将下面的内容放入Product.php文件中:

app/Product.php

  1. namespace App; 
  2.  
  3.  
  4.  
  5. use Illuminate\Database\Eloquent\Model; 
  6.  
  7.  
  8.  
  9. class Product extends Model 
  10.  
  11. { 
  12.  
  13.     /** 
  14.  
  15.      * The attributes that are mass assignable. 
  16.  
  17.      * 
  18.  
  19.      * @var array 
  20.  
  21.      */ 
  22.  
  23.     protected $fillable = [ 
  24.  
  25.         'name', 'detail' 
  26.  
  27.     ]; 
  28.  
  29. } 

第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

  1. <!DOCTYPE html> 
  2.  
  3. <html> 
  4.  
  5. <head> 
  6.  
  7.     <title>Laravel 5.6 CRUD Application</title> 
  8.  
  9.     <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"> 
  10.  
  11. </head> 
  12.  
  13. <body> 
  14.  
  15.  
  16.  
  17. <div class="container"> 
  18.  
  19.     @yield('content') 
  20.  
  21. </div> 
  22.  
  23.  
  24.  
  25. </body> 
  26.  
  27. </html> 

resources/views/products/index.blade.php

  1. @extends('products.layout') 
  2.  
  3.  
  4.  
  5. @section('content') 
  6.  
  7.     <div class="row"> 
  8.  
  9.         <div class="col-lg-12 margin-tb"> 
  10.  
  11.             <div class="pull-left"> 
  12.  
  13.                 <h2>Laravel 5.6 CRUD Example from scratch</h2> 
  14.  
  15.             </div> 
  16.  
  17.             <div class="pull-right"> 
  18.  
  19.                 <a class="btn btn-success" href="{{ route('products.create') }}"> Create New Product</a> 
  20.  
  21.             </div> 
  22.  
  23.         </div> 
  24.  
  25.     </div> 
  26.  
  27.  
  28.  
  29.     @if ($message = Session::get('success')) 
  30.  
  31.         <div class="alert alert-success"> 
  32.  
  33.             <p>{{ $message }}</p> 
  34.  
  35.         </div> 
  36.  
  37.     @endif 
  38.  
  39.  
  40.  
  41.     <table class="table table-bordered"> 
  42.  
  43.         <tr> 
  44.  
  45.             <th>No</th> 
  46.  
  47.             <th>Name</th> 
  48.  
  49.             <th>Details</th> 
  50.  
  51.             <th width="280px">Action</th> 
  52.  
  53.         </tr> 
  54.  
  55.         @foreach ($products as $product) 
  56.  
  57.         <tr> 
  58.  
  59.             <td>{{ ++$i }}</td> 
  60.  
  61.             <td>{{ $product->name }}</td> 
  62.  
  63.             <td>{{ $product->detail }}</td> 
  64.  
  65.             <td> 
  66.  
  67.                 <form action="{{ route('products.destroy',$product->id) }}" method="POST"> 
  68.  
  69.  
  70.  
  71.                     <a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a> 
  72.  
  73.                     <a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a> 
  74.  
  75.  
  76.  
  77.                     @csrf 
  78.  
  79.                     @method('DELETE') 
  80.  
  81.  
  82.  
  83.      
  84.  
  85.                     <button type="submit" class="btn btn-danger">Delete</button> 
  86.  
  87.                 </form> 
  88.  
  89.             </td> 
  90.  
  91.         </tr> 
  92.  
  93.         @endforeach 
  94.  
  95.     </table> 
  96.  
  97.  
  98.  
  99.     {!! $products->links() !!} 
  100.  
  101.  
  102.  
  103. @endsection 

resources/views/products/show.blade.php

  1. @extends('products.layout') 
  2.  
  3.  
  4.  
  5. @section('content') 
  6.  
  7.     <div class="row"> 
  8.  
  9.         <div class="col-lg-12 margin-tb"> 
  10.  
  11.             <div class="pull-left"> 
  12.  
  13.                 <h2> Show Product</h2> 
  14.  
  15.             </div> 
  16.  
  17.             <div class="pull-right"> 
  18.  
  19.                 <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> 
  20.  
  21.             </div> 
  22.  
  23.         </div> 
  24.  
  25.     </div> 
  26.  
  27.  
  28.  
  29.     <div class="row"> 
  30.  
  31.         <div class="col-xs-12 col-sm-12 col-md-12"> 
  32.  
  33.             <div class="form-group"> 
  34.  
  35.                 <strong>Name:</strong> 
  36.  
  37.                 {{ $product->name }} 
  38.  
  39.             </div> 
  40.  
  41.         </div> 
  42.  
  43.         <div class="col-xs-12 col-sm-12 col-md-12"> 
  44.  
  45.             <div class="form-group"> 
  46.  
  47.                 <strong>Details:</strong> 
  48.  
  49.                 {{ $product->detail }} 
  50.  
  51.             </div> 
  52.  
  53.         </div> 
  54.  
  55.     </div> 
  56.  
  57. @endsection 

resources/views/products/create.blade.php

  1. @extends('products.layout') 
  2.  
  3. @section('content') 
  4.  
  5.     <div class="row"> 
  6.  
  7.         <div class="col-lg-12 margin-tb"> 
  8.  
  9.             <div class="pull-left"> 
  10.  
  11.                 <h2>Add New Product</h2> 
  12.  
  13.             </div> 
  14.  
  15.             <div class="pull-right"> 
  16.  
  17.                 <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> 
  18.  
  19.             </div> 
  20.  
  21.         </div> 
  22.  
  23.     </div> 
  24.  
  25.  
  26.  
  27.     @if ($errors->any()) 
  28.  
  29.         <div class="alert alert-danger"> 
  30.  
  31.             <strong>Whoops!</strong> There were some problems with your input.<br><br> 
  32.  
  33.             <ul> 
  34.  
  35.                 @foreach ($errors->all() as $error) 
  36.  
  37.                     <li>{{ $error }}</li> 
  38.  
  39.                 @endforeach 
  40.  
  41.             </ul> 
  42.  
  43.         </div> 
  44.  
  45.     @endif 
  46.  
  47.  
  48.  
  49.     <form action="{{ route('products.store') }}" method="POST"> 
  50.  
  51.         @csrf 
  52.  
  53.  
  54.  
  55.          <div class="row"> 
  56.  
  57.             <div class="col-xs-12 col-sm-12 col-md-12"> 
  58.  
  59.                 <div class="form-group"> 
  60.  
  61.                     <strong>Name:</strong> 
  62.  
  63.                     <input type="text" name="name" class="form-control" placeholder="Name"> 
  64.  
  65.                 </div> 
  66.  
  67.             </div> 
  68.  
  69.             <div class="col-xs-12 col-sm-12 col-md-12"> 
  70.  
  71.                 <div class="form-group"> 
  72.  
  73.                     <strong>Detail:</strong> 
  74.  
  75.                     <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea> 
  76.  
  77.                 </div> 
  78.  
  79.             </div> 
  80.  
  81.             <div class="col-xs-12 col-sm-12 col-md-12 text-center"> 
  82.  
  83.                     <button type="submit" class="btn btn-primary">Submit</button> 
  84.  
  85.             </div> 
  86.  
  87.         </div> 
  88.  
  89.  
  90.  
  91.     </form> 
  92.  
  93.  
  94.  
  95. @endsection 

resources/views/products/edit.blade.php

  1. @extends('products.layout') 
  2.  
  3. @section('content') 
  4.  
  5.     <div class="row"> 
  6.  
  7.         <div class="col-lg-12 margin-tb"> 
  8.  
  9.             <div class="pull-left"> 
  10.  
  11.                 <h2>Edit Product</h2> 
  12.  
  13.             </div> 
  14.  
  15.             <div class="pull-right"> 
  16.  
  17.                 <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> 
  18.  
  19.             </div> 
  20.  
  21.         </div> 
  22.  
  23.     </div> 
  24.  
  25.  
  26.  
  27.     @if ($errors->any()) 
  28.  
  29.         <div class="alert alert-danger"> 
  30.  
  31.             <strong>Whoops!</strong> There were some problems with your input.<br><br> 
  32.  
  33.             <ul> 
  34.  
  35.                 @foreach ($errors->all() as $error) 
  36.  
  37.                     <li>{{ $error }}</li> 
  38.  
  39.                 @endforeach 
  40.  
  41.             </ul> 
  42.  
  43.         </div> 
  44.  
  45.     @endif 
  46.  
  47.  
  48.  
  49.     <form action="{{ route('products.update',$product->id) }}" method="POST"> 
  50.  
  51.         @csrf 
  52.  
  53.         @method('PUT') 
  54.  
  55.  
  56.  
  57.          <div class="row"> 
  58.  
  59.             <div class="col-xs-12 col-sm-12 col-md-12"> 
  60.  
  61.                 <div class="form-group"> 
  62.  
  63.                     <strong>Name:</strong> 
  64.  
  65.                     <input type="text" name="name" value="{{ $product->name }}" class="form-control" placeholder="Name"> 
  66.  
  67.                 </div> 
  68.  
  69.             </div> 
  70.  
  71.             <div class="col-xs-12 col-sm-12 col-md-12"> 
  72.  
  73.                 <div class="form-group"> 
  74.  
  75.                     <strong>Detail:</strong> 
  76.  
  77.                     <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{ $product->detail }}</textarea> 
  78.  
  79.                 </div> 
  80.  
  81.             </div> 
  82.  
  83.             <div class="col-xs-12 col-sm-12 col-md-12 text-center"> 
  84.  
  85.               <button type="submit" class="btn btn-primary">Submit</button> 
  86.  
  87.             </div> 
  88.  
  89.         </div> 
  90.  
  91.  
  92.  
  93.     </form> 
  94.  
  95.  
  96.  
  97. @endsection 

现在,我们准备运行我们的crud应用程序的例子,所以运行以下命令快速运行:

php artisan serve

最后你就可以在浏览器上打开下面的网址进行查看测试:

http://localhost:8000/products

本篇文章就是关于Laravel 5.6中的CURD操作即创建,读取,更新和删除操作,希望对需要的朋友有所帮助!

Tags: Laravel 5 6 CURD

分享到: