为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
laravel

在这里,我将向您展示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"}



点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
PHP开发工程师
手记
粉丝
8
获赞与收藏
17

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消