laravel启动方法在模型中不起作用

Softya

您好,我使用laravel 8,我尝试在创建或更新后执行一些代码,例如模型是否已创建集created_by = Auth :: user()-> id ect,这是我的模型代码

<?php
namespace App\Models;
use Helper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Auth;
use App\Models\User;
use Spatie\Activitylog\Traits\LogsActivity;
class Account extends Model
{
    use Notifiable,SoftDeletes,LogsActivity;
    protected static $logAttributes = 
    [
        'number',
        'name',
        'foreign_name',
        'main_account_id',
        'user_id',
        'account_state_id',
        'note',
        'balance_limit',
        'approved_state_id'
    ];
    protected static $logName = 'accounts';
    protected static $logAttributesToIgnore = [ 'updated_at'];
    protected $table = 'accounts';
    protected $fillable = 
    [
        'number',
        'name',
        'foreign_name',
        'main_account_id',
        'user_id',
        'account_state_id',
        'note',
        'approved_state_id',
        'balance_limit',
        'created_by','deleted_by'
    ];
    public static function boot()
    {
        parent::boot();
        self::creating(function($model){
            $model->created_by = Auth::user()->id; // this code work
        });
        static::updating(function ($model) {
            dd('asdf'); // this not working
        });

        self::deleting(function($model){
            dd($model); // this not working 
        });
        self::restoring(function($model){
            dd($model); // this not working 
        });
    }
}

现在的代码

static::updating(function ($model) {
    dd('asdf'); // this not working
});

当我也在删除和还原dd('asdf')中更新任何帐户时无法正常工作;不工作

任何帮助在这里谢谢..

尼基尔·尼迪约德

请尝试覆盖引导启动的引导方法。它是laravel 7的更新。

protected static function booted()
{
    static::created(function ($user) {
        //
    });
}

在此处使用闭包检查官方文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章