猿问

boot() 无法在 create() 回调中使用变量

在创建用户时,User.php我试图定义一个 UUID,创建一个目录,然后将其添加到数据库上的用户。该文件夹使用 UUID 成功创建,但不会传递到creating()回调中。


/**

    * Create the user when this class is called

    *

    * @return void

    */

public static function boot()

{

    // Setup parent

    parent::boot();


    // Create UUID

    $uuid = Str::uuid();


    // Create user directory on S3

    Storage::disk('s3')->makeDirectory('users/' . $uuid);


    // Assign UUID to new user

    self::creating(function ($model) {

        $model->id = $uuid;

    });

}

$model->id = $uuid;在这里未定义并且没有任何值。


慕哥6287543
浏览 83回答 1
1回答

qq_遁去的一_1

像这样注入$uuid闭包:self::creating(function ($model) use ($uuid) {         $model->id = $uuid;     });
随时随地看视频慕课网APP
我要回答