Laravel 8 是一个开源的 PHP 框架。在本文中,我们将看到Laravel 4 CRUD 应用程序教程的实现。如今,每个应用程序都有一个通用的操作CRUD。CRUD 代表创建、读取、更新和删除。
创建迁移
因为我们将在 laravel 中创建 CRUD(创建、读取、更新、删除)应用程序。所以让我们以product为例。
在这里,我们必须创建一个创建产品表的迁移。
$ php artisan make:migration create_products_table --create=products
在终端上运行此命令后,您将看到将在以下路径/database/migrations
中创建一个文件。
打开{timestamp}_create_products_table.php
文件并将完整的代码写入其中。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('amount');
$table->text('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
现在,您必须运行此给定命令来迁移上述文件。
$ php artisan migrate
以上命令将在数据库中创建一个产品表。
添加应用程序控制器和模型
接下来,我们应该创建一个新的控制器作为ProductController。我们将通过运行给定的命令来创建一个资源控制器。
$ php artisan make:controller ProductController --resource --model=Product
该命令将创建一个 ProductController
,即一个资源控制器和一个模型。你可以在这个路径app/Http/Controllers/ProductController.php
和app/Models/Product.php
的模型中找到创建的文件
在这个资源控制器中,我们将有以下方法:
- index()
- create()
- store()
- show()
- edit()
- update()
- delete()
从这个路径 app/Models/Product.php
打开模型并编写这个更新的代码。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name', 'description', 'amount'
];
}
打开app/Http/Controllers/ProductController.php
并粘贴此更新后的代码。
<?php
namespace App\Http\Controllers;
use App\Models\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',
'description' => 'required',
'amount' => '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',
'description' => 'required',
'amount' => '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');
}
}
应用资源路由设置
从路径routes/web.php
打开路由配置文件web.php
。
将以下代码添加到其中。
use App\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);
如果我们有兴趣查看与 Resource Controller
关联的路由,请返回终端并输入命令
$ php artisan route:list
添加应用程序布局文件
在这里面我们需要创建产品的布局文件。让我们在路径resources/views/
下创建一个新文件夹products
然后在其中创建 CRUD 应用程序的Blade文件。
所以我们必须创建以下Blade文件:
- layout.blade.php
- index.blade.php
- create.blade.php
- edit.blade.php
- show.blade.php
打开文件resources/views/products/layout.blade.php
并将以下给定代码粘贴到其中。
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 CRUD Application Tutorial - Online Web Tutor</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" style="margin-top:100px;">
@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">
<h3>Laravel 8 CRUD Application Tutorial - Online Web Tutor</h3>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('products.create') }}"> Add 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" style="margin-top:20px;">
<tr>
<th>No</th>
<th>Name</th>
<th>Amount</th>
<th>Description</th>
<th width="280px">Action</th>
</tr>
@foreach ($products as $product)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->amount }}</td>
<td>{{ $product->description }}</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/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 are some issues.<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>Amount:</strong>
<input type="number" name="amount" class="form-control" placeholder="Amount">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
<textarea class="form-control" style="height:150px" name="description" placeholder="Description"></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 are some issues.<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>Amount:</strong>
<input type="number" name="amount" value="{{ $product->amount }}" class="form-control" placeholder="Amount">
</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="description"
placeholder="Detail">{{ $product->description }}</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/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>Amount:</strong>
{{ $product->amount }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
{{ $product->description }}
</div>
</div>
</div>
@endsection
最后,我们准备运行 Laravel 8 CRUD 应用程序。
应用测试
打开项目到终端并输入命令启动开发服务器
$ php artisan serve
打开网址:http://127.0.0.1:8000/products
当我们点击添加新产品按钮时,它将打开 http://127.0.0.1:8000/products/create
当我们从 Create Form 添加产品时,我们将在 /products URL 看到产品列表。在列表中,我们将看到编辑和删除按钮。
我们希望本文能帮助您以非常详细的方式了解 Laravel 8 CRUD 应用程序教程。
评论区