我有不同的模型,描述数据库中的不同表 .

我在这些课程中使用这个特性

trait ApplicationModelTrait
 {
     protected static $_currentModel = null;

     protected $currentModel;

     public static function setCurrentModel(ApplicationModel $model) {
         static::$_currentModel = $model;
     }   

     public function __construct(array $attributes = [], ApplicationModel $model = null) {
         parent::__construct($attributes);

         $this->currentModel = $model !== null ? $model : static::$_currentModel;

         if($this->currentModel === null) throw new \InvalidArgumentException('No model passed');
     }   
 }

所有型号都相似 . 唯一的区别是数据库中存在哪些字段用于每个模型,因此我在单独的配置和主模型类中描述这些字段 ApplicationModel 有一些方法可以处理不同的表 . 例如

public function getInstanceTable() {
     return 'application_model_instances_' . $this->name;
}

public function getCommentsTable() {
    return 'application_model_instance_' . $this->name . '_comments';
}

其中 application_model_instances_{name} 包含(显然)此模型的实例, application_model_instance_{name}_comments 包含此模型实例的注释 .

一切都很好,除了事件 .

当我向模型实例添加注释时,我作为参数传递当前模型

$comment = new ApplicationModelInstanceComment([], $this->currentModel);
$comment->text = $request->input('comment');
// etc.

保存评论后,我希望它能够通过websocket立即传送到用户浏览器

event(new CommentCreated($ comment,$ this)); // this 表示模型实例类

最后,事件

namespace App\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Models\Application\ApplicationModelInstanceComment;
use App\Models\Application\ApplicationModelInstance;

class CommentCreated implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;
    public $comment;
    public $instance;

    public function __construct(ApplicationModelInstanceComment $comment, ApplicationModelInstance $instance)
    {
        $this->comment = $comment;
        $this->instance = $instance;
    }

    public function broadcastOn() {
        $notifiableUsers = $this->instance->getNotifiableUsers(); // this method is used to fetch list of users who must get this comment in their browser
        $channels = [];
        foreach($notifiableUsers as $user) {
            $channels[] = new PrivateChannel('user.' . $user->id);
        }
        return $channels;
    }
}

但是当我添加评论Laravel时,我的代码会引发异常(这是我在Google Chrome中的网络标签中看到的,因为请求是通过ajax完成的:

Whoops, looks like something went wrong.

1/1 InvalidArgumentException in ApplicationModelTrait.php line 20: No model passed

in ApplicationModelTrait.php line 20
at ApplicationModelInstanceComment->__construct() in SerializesAndRestoresModelIdentifiers.php line 45
at CommentCreated->getRestoredPropertyValue(object(ModelIdentifier)) in SerializesModels.php line 41
at CommentCreated->__wakeup()
at unserialize('O:38:"Illuminate\\Broadcasting\\BroadcastEvent":4:{s:5:"event";O:25:"App\\Events\\CommentCreated":3:{s:7:"comment";O:45:"Illuminate\\Contracts\\Database\\ModelIdentifier":2:{s:5:"class";s:54:"App\\Models\\Application\\ApplicationModelInstanceComment";s:2:"id";i:68;}s:8:"instance";O:45:"Illuminate\\Contracts\\Database\\ModelIdentifier":2:{s:5:"class";s:47:"App\\Models\\Application\\ApplicationModelInstance";s:2:"id";i:11;}s:6:"socket";N;}s:10:"connection";N;s:5:"queue";N;s:5:"delay";N;}') in CallQueuedHandler.php line 95
at CallQueuedHandler->failed(array('commandName' => 'Illuminate\\Broadcasting\\BroadcastEvent', 'command' => 'O:38:"Illuminate\\Broadcasting\\BroadcastEvent":4:{s:5:"event";O:25:"App\\Events\\CommentCreated":3:{s:7:"comment";O:45:"Illuminate\\Contracts\\Database\\ModelIdentifier":2:{s:5:"class";s:54:"App\\Models\\Application\\ApplicationModelInstanceComment";s:2:"id";i:68;}s:8:"instance";O:45:"Illuminate\\Contracts\\Database\\ModelIdentifier":2:{s:5:"class";s:47:"App\\Models\\Application\\ApplicationModelInstance";s:2:"id";i:11;}s:6:"socket";N;}s:10:"connection";N;s:5:"queue";N;s:5:"delay";N;}'), object(InvalidArgumentException)) in Job.php line 158
at Job->failed(object(InvalidArgumentException)) in FailingJob.php line 33

我在创建评论时将模型传递给ApplicationModelInstanceComment,所以一切都很好 . 反序列化存在一些问题,但我不知道如何处理这个问题 .