日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

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

發(fā)布時(shí)間:2023/12/1 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 curd操作php代码,Laravel 5.6中的CURD操作(代码示例详解) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在本篇文章中,我將給大家分享laravel 5.6版本中的基本crud(創(chuàng)建,讀取,更新和刪除)應(yīng)用程序模塊。你可以按照下面的步驟在laravel 5.6中創(chuàng)建CRUD應(yīng)用程序。

Laravel是一個(gè)流行的開源PHP MVC框架,具有許多高級(jí)開發(fā)功能。如果你是laravel 5.6應(yīng)用程序中的學(xué)習(xí)者或初學(xué)者,更多地了解或?qū)W習(xí)crud應(yīng)用程序總是有很大幫助的。(相關(guān)laravel視頻教程:《最新laravel商城實(shí)戰(zhàn)視頻教程》)

下面我將創(chuàng)建insert(插入)、update(更新)、delete(刪除)和view(查看)和產(chǎn)品的分頁示例。你只需創(chuàng)建新產(chǎn)品,查看產(chǎn)品,編輯產(chǎn)品并從列表中刪除產(chǎn)品即可。

第1步:安裝Laravel 5.6

可以在終端中運(yùn)行 create-project 命令來安裝 Laravel:composer create-project --prefer-dist laravel/laravel blog

第2步:數(shù)據(jù)庫配置

完成安裝后,我們將為laravel 5.6的crud應(yīng)用程序進(jìn)行數(shù)據(jù)庫配置,例如數(shù)據(jù)庫名稱,用戶名,密碼等。所以,讓我們打開.env文件并填寫相關(guān)信息,如下:

.envDB_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步:創(chuàng)建產(chǎn)品表和模型

我們將為產(chǎn)品創(chuàng)建crud應(yīng)用程序。所以我們必須使用Laravel 5.6 php artisan命令創(chuàng)建產(chǎn)品表的遷移(migrations),首先使用以下命令:php artisan make:migration create_products_table --create=products

在執(zhí)行此命令之后,你可以在路徑database/migrations中找到一個(gè)文件,并且必須將以下代碼放在migrations文件中以用于創(chuàng)建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()

{

Schema::dropIfExists('products');

}

}

第4步:添加resource路由

在這個(gè)步驟中,我們需要為產(chǎn)品crud應(yīng)用添加resource路由。所以打開routes / web.php文件并添加以下路由。

routes/web.phpRoute::resource('products','ProductController');

第5步:創(chuàng)建ProductController

現(xiàn)在,我們應(yīng)該創(chuàng)建一個(gè)新的控制器ProductController。因此要運(yùn)行以下命令并創(chuàng)建新的控制器。下面的控制器用于創(chuàng)建resource控制器。

創(chuàng)建ProductControllerphp artisan make:controller ProductController --resource --model=Product

在下面的命令之后,你將在這個(gè)路徑app/Http/Controllers/ProductController.php中找到新的文件。

在這個(gè)控制器中,默認(rèn)情況下將創(chuàng)建7個(gè)方法如下所示:

1)index()

2)create()

3)store()

4)show()

5)edit()

6)update()

7)destroy()

因此,讓我們復(fù)制下面的代碼并將其放到ProductController.php文件中。

app/Http/Controllers/ProductController.php<?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)

{

$product->delete();

return redirect()->route('products.index')

->with('success','Product deleted successfully');

}

}

OK,運(yùn)行下面命令后,你會(huì)找到app/Product.php,并將下面的內(nèi)容放入Product.php文件中:

app/Product.php<?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步:創(chuàng)建Blade文件

現(xiàn)在我們進(jìn)入最后一步。在這一步中,我們只需要?jiǎng)?chuàng)建blade文件。所以我們主要需要?jiǎng)?chuàng)建布局文件,然后創(chuàng)建新的文件夾“products”,然后創(chuàng)建crud app的blade文件。最后需要?jiǎng)?chuàng)建以下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

讓我們創(chuàng)建下面的文件,并放入下面的代碼。

resources/views/products/layout.blade.php

Laravel 5.6 CRUD Application

@yield('content')

resources/views/products/index.blade.php@extends('products.layout')

@section('content')

Laravel 5.6 CRUD Example from scratch

Create New Product

@if ($message = Session::get('success'))

{{ $message }}

@endif

NoNameDetailsAction

@foreach ($products as $product)

{{ ++$i }}{{ $product->name }}{{ $product->detail }}

Show

Edit

@csrf

@method('DELETE')

Delete

@endforeach

{!! $products->links() !!}

@endsection

resources/views/products/show.blade.php@extends('products.layout')

@section('content')

Show Product

Back

Name:

{{ $product->name }}

Details:

{{ $product->detail }}

@endsection

resources/views/products/create.blade.php@extends('products.layout')

@section('content')

Add New Product

Back

@if ($errors->any())

Whoops! There were some problems with your input.

@foreach ($errors->all() as $error)

{{ $error }}

@endforeach

@endif

@csrf

Name:

Detail:

Submit

@endsection

resources/views/products/edit.blade.php@extends('products.layout')

@section('content')

Edit Product

Back

@if ($errors->any())

Whoops! There were some problems with your input.

@foreach ($errors->all() as $error)

{{ $error }}

@endforeach

@endif

@csrf

@method('PUT')

Name:

Detail:

{{ $product->detail }}

Submit

@endsection

現(xiàn)在,我們準(zhǔn)備運(yùn)行我們的crud應(yīng)用程序的例子,所以運(yùn)行以下命令快速運(yùn)行:php artisan serve

最后你就可以在瀏覽器上打開下面的網(wǎng)址進(jìn)行查看測(cè)試:http://localhost:8000/products

本篇文章就是關(guān)于Laravel 5.6中的CURD操作即創(chuàng)建,讀取,更新和刪除操作,希望對(duì)需要的朋友有所幫助!

總結(jié)

以上是生活随笔為你收集整理的curd操作php代码,Laravel 5.6中的CURD操作(代码示例详解)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。