在本篇文章中,我將給大家分享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
- DB_CONNECTION=mysql
- DB_HOST=127.0.0.1
- DB_PORT=3306
- DB_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表。
- <?php
- use 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()
- //Vevb.com
- {
- 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
- namespace 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)
- {
- //Vevb.com
- $product->delete();
- return redirect()->route('products.index')
- ->with('success','Product deleted successfully');
- }
- }
OK,運行下面命令后,你會找到app/Product.php,并將下面的內容放入Product.php文件中:
app/Product.php
- namespace 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操作即創建,讀取,更新和刪除操作,希望對需要的朋友有所幫助!
新聞熱點
疑難解答