代码之家  ›  专栏  ›  技术社区  ›  Stan Barrows

Laravel Nova:生成没有字段的模型值

  •  1
  • Stan Barrows  · 技术社区  · 7 年前

    例如,在每次存储/更新之后,我想用当前已验证的用户更新created\u by值。

    $model->created_by = aut()->user()->id
    
    1 回复  |  直到 7 年前
        1
  •  6
  •   r00t    7 年前

    在您的模型中,您可以添加一个boot()方法,该方法将允许您管理 saving 事件

    可用事件包括 creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored

    public static function boot()
    {
        parent::boot();
    
        self::saving(function($model){
            $model->created_by = auth()->user()->id;
        });
    }
    

    推荐文章