当先锋百科网

首页 1 2 3 4 5 6 7

在这里,我将向您展示laravel 8观察者示例。该示例将帮助您如何使用laravel 8模型观察器。
Laravel观察者用于对模型事件监听器进行分组。 Laravel Observers将侦听模型方法(例如创建,更新和删除)的事件。
Eloquent hook

  • Retrieved:查询数据后。
  • Creating:在创建数据之前。
  • Created:创建数据后。
  • Updating:在更新数据之前。
  • Updated:更新数据后。
  • Saving:保存数据之前(创建或更新)。
  • Saved:保存数据后(创建或更新)。
  • Deleting:在删除数据或软删除之前。
  • Deleted:删除或软删除数据后。
  • Restoring:软删除数据恢复前。
  • Restoring:软删除数据恢复后。

现在在这里,我们将看到一个简单的示例,我有一个产品模型(Product Model),它具有name,slug,price和unique_id列。因此,我需要创建一个仅以价格命名的记录。但是当它创建时,我需要根据名称生成slug并自动生成unique_id。
app/Models/Product.php

<?php

namespace App\Observers;
use App\Models\Product;
class ProductObserver{
    /**
     * Handle the Product "created" event.
     *
     * @param  \App\Models\Product  $product
     * @return void
     */
    public function creating(Product $product)
    {
        $product->slug = \Str::slug($product->name);
    }
    /**
     * Handle the Product "created" event.
     *
     * @param  \App\Models\Product  $product
     * @return void
     */
    public function created(Product $product)
    {
        $product->unique_id = 'PR-'.$product->id;
        $product->save();
    }
    /**
     * Handle the Product "updated" event.
     *
     * @param  \App\Models\Product  $product
     * @return void
     */
    public function updated(Product $product){

    }

    /**
     * Handle the Product "deleted" event.
     *
     * @param  \App\Models\Product  $product
     * @return void
     */
    public function deleted(Product $product){
    }


    /**
     * Handle the Product "restored" event.
     *
     * @param  \App\Models\Product  $product
     * @return void
     */
    public function restored(Product $product){
    }

  

    /**
     * Handle the Product "force deleted" event.
     *
     * @param  \App\Models\Product  $product
     * @return void
     */
    public function forceDeleted(Product $product){
    }
}

在提供者上注册Observers类。
app/Providers/EventServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Observers\ProductObserver;
use App\Models\Product;
class EventServiceProvider extends ServiceProvider{

    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];
    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        Product::observe(ProductObserver::class);
    }
}

创建示例路由:
routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('product', [ProductController::class, 'index']);

创建控制器路由:
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()

    {
        $product = Product::create([

            'name' => 'Platinum 1',

            'price' => 10

        ]);

        dd($product);

    }

}


现在您可以运行项目并查看。