Laravel 5.4 关系未在模型启动方法中加载

Abh*_*bhi 6 php laravel laravel-5.4

我有一个名为“民意调查”的模型。在 Poll 模型中,我定义了一个启动方法,如下所示:

\n\n
public static function boot()\n{\n    parent::boot();\n\n    self::created(function($model){\n       // dd($model);\n       $speakers = $model->speakers()->get();\n       // dd($speakers);\n\n       // What I want to do here is: create poll options relation from speakers as follows\n       // $poll->poll_options()->create([\n       //     'option' => $speaker->name,\n       // ]);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我正在添加扬声器关系,它运行完美。

\n\n

但是在这个启动方法中,在 self::created 中,如果我尝试获取扬声器关系,它总是空的(dd($speakers)行)。是否是因为引导方法在模型保存到数据库后立即运行并且关系根本没有保存?

\n\n

我在代码中提到的行中获得了新创建的模型:dd($model)。

\n\n

更新

\n\n

我也尝试过事件。\n我的投票模型:

\n\n
<?php\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Backpack\\CRUD\\CrudTrait;\nuse Cookie;\nuse App\\Events\\PollCreated;\n\nclass Poll extends Model\n{\n  ........\n  protected $events = [\n        'created' => PollCreated::class,\n    ];\n  .......\n\n  public function speakers()\n  {\n    return $this->belongsToMany('App\\Models\\Speaker','poll_speaker','poll_id','speaker_id');\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

应用程序/事件/PollCreated.php:

\n\n

\n\n

namespace App\\Events;\n\nuse App\\Models\\Poll;\nuse Illuminate\\Queue\\SerializesModels;\n\nclass PollCreated\n{\n  use SerializesModels;\n\n  public $poll;\n\n  /**\n   * Create a new event instance.\n   *\n   * @param  Poll  $poll\n   * @return void\n   */\n  public function __construct(Poll $poll)\n  {\n      // $this->poll = $poll;\n      $event = $poll->event()->first();\n      // dd($event);\n      // dd($poll->speakers()->get());\n      // dd($poll->load('speakers'));\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

在这里我也没有得到发言者,在行中: dd($poll->speakers()->get());

\n\n

我的扬声器型号:

\n\n
    <?php\n\n    namespace App\\Models;\n\n    use Illuminate\\Database\\Eloquent\\Model;\n    use Backpack\\CRUD\\CrudTrait;\n\n    class Speaker extends Model\n    {\n    use CrudTrait;\n    \xe2\x80\xa6\xe2\x80\xa6..\n\n        public function polls()\n        {\n            return $this->belongsToMany('App\\Models\\Poll');\n        }\n    \xe2\x80\xa6\xe2\x80\xa6..\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

Art*_*äpp 3

问题在于时间安排,因为必须始终创建模型,然后才能将其设置为多对多关系。因此,在事件期间的多对多关系中,不可能created已经设置关系,因为事件created总是在关系之前引发。

任何寻找解决方案的人都可以尝试chelout/laravel-relationship-events包,因为这会将关系事件添加到模型中。

可以肯定的是,我通过用户和计算机的简单应用程序对此进行了测试。

用户.php

class User extends Model
{
    use HasBelongsToManyEvents;

    public static function boot() {
        parent::boot();

        self::created(function($model){
           Log::info('user::created');
        });

        static::belongsToManyAttaching(function ($relation, $parent, $ids) {
            $ids = implode(' & ', $ids);
            Log::info("Attaching {$relation} {$ids} to user.");
        });

        static::belongsToManyAttached(function ($relation, $parent, $ids) {
            $ids = implode(' & ', $ids);
            Log::info("Computers {$ids} have been attached to user.");
        });
    }

    public function computers() {
        return $this->belongsToMany(Computer::class, 'user_computers');
    }
}
Run Code Online (Sandbox Code Playgroud)

计算机类反之亦然。对于以下代码:

$user = User::create();

$user->computers()->attach([
    Computer::create()->id,
    Computer::create()->id
]);
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

user::created  
computer::created  
computer::created  
Attaching computers 69 & 70 to user.  
Computers 69 & 70 have been attached to user.  
Run Code Online (Sandbox Code Playgroud)