Laravel 8 - 模型事件

Laravel 8 - 模型事件 首页 / Laravel8入门教程 / 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"}

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

技术领导力实战笔记 -〔TGO鲲鹏会〕

视觉笔记入门课 -〔高伟〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

运维监控系统实战笔记 -〔秦晓辉〕

快速上手C++数据结构与算法 -〔王健伟〕

深入拆解消息队列47讲 -〔许文强〕

结构思考力 · 透过结构看思考 -〔李忠秋〕

程序员职业规划手册 -〔雪梅〕

Midjourney入门实践课 -〔Jovi〕

好记忆不如烂笔头。留下您的足迹吧 :)