無涯教程: Laravel 8 - 模型事件

在這裏,我將向您展示laravel 8模型事件。Laravel提供eloquent模型事件列表,每個模型事件都有自己的函數。

  • creating - 插入前調用。
  • creating - 插入後調用。
  • updating - 更新前調用。
  • updated - 更新後調用。
  • deleting - 刪除前調用。
  • deleted - 刪除後調用。
  • retriveved - 從數據庫查詢數據時調用。
  • saving - 創建或更新前調用。
  • saved - 創建或更新後調用。
  • restoring - 在恢復前調用。
  • restored - 恢復後調用。
  • replicating - 調用重複數據。

創建Product Model

在這裏,我們將使用事件創建Product模型。所以讓我們創建並寫入以下代碼:
app/Models/Product.php

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Log;
use Str;
 
class Product extends Model
{
 use HasFactory;
 
 protected $fillable = [
 'name', 'slug', 'detail'
 ];
 
 /**
     * Write code on Method
     *
     * @return response()
     */
 public static function boot() {
 
        parent::boot();
 
 /**
         * Write code on Method
         *
         * @return response()
         */
 static::creating(function($item) { 
 Log::info('Creating event call: '.$item); 
 
            $item->slug = Str::slug($item->name);
 });
 
 /** 
         * Write code on Method
         *
         * @return response()
         */
 static::created(function($item) { 
 /*
                Write Logic Here
            */ 
 
 Log::info('Created event call: '.$item);
 });
 
 /**
         * Write code on Method
         *
         * @return response()
         */
 static::updating(function($item) { 
 Log::info('Updating event call: '.$item); 
 
            $item->slug = Str::slug($item->name);
 });
 
 /**
         * Write code on Method
         *
         * @return response()
         */
 static::updated(function($item) { 
 /*
                Write Logic Here
            */ 
 
 Log::info('Updated event call: '.$item);
 });
 
 /**
         * Write code on Method
         *
         * @return response()
         */
 static::deleted(function($item) { 
 Log::info('Deleted event call: '.$item); 
 });
 }
}


創建記錄:Creating和Created事件
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::create([
 'name' => 'silver',
 'detail' => 'This is silver'
 ]);
 
        dd('done');
 }
}

輸出日誌文件:

[2020-10-20 14:37:26] local.INFO: Creating event call: {"name":"silver","detail":"This is silver"} 
[2020-10-20 14:37:26] local.INFO: Created event call: {"name":"silver","detail":"This is silver","slug":"silver","updated_at":"2020-10-20T14:37:26.000000Z","created_at":"2020-10-20T14:37:26.000000Z","id":5} 

更新記錄:Updating和Updated事件
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::find(5)->update([
 'name' => 'silver updated',
 'detail' => 'This is silver'
 ]);
 
        dd('done');
 }
}

輸出日誌文件:

[2020-10-20 14:39:04] local.INFO: Updating event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:37:26.000000Z","slug":"silver"} 
[2020-10-20 14:39:04] local.INFO: Updated event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:39:04.000000Z","slug":"silver-updated"} 

刪除記錄:Deleted事件
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::find(5)->delete();
 
        dd('done');
 }
}

輸出日誌文件:

[2020-10-21 03:14:45] local.INFO: Deleted event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:39:04.000000Z","slug":"silver-updated"} 

 

https://www.learnfk.com/article-how-to-use-model-events-in-laravel-8example

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章