如何使用App服务提供商

时间:2016-12-04 07:53:26

标签: php laravel

我被称为所有模型账户,发布,评论,任务和互动

但我有一个错误。这是错误:

  

类型错误:参数1传递给   App \ Providers \ AppServiceProvider :: App \ Providers {closure}()必须是   App \ Account的实例,App \ Post给出的实例

这是我的代码:

public function boot(){
    Post::saved(function(Account $account, Account $from, Post $post){
        if ($post->isIntractable()) {
            $interaction = Interaction::add($post, $from, $account);
            Task::add($interaction);
        }
    });

    Comment::saved(function ($account, $from, Comment $comment){
        $interaction = Interaction::add($comment, $from, $account);
        Task::add($interaction);

        $raw_data = json_decode($comment->raw_data, true);
        $replies = Comment::makeOrUpdateGraphEdge(array_get($raw_data, 'comments'));
        foreach ($replies as $reply) {
            $raw_data = json_decode($reply->raw_data, true);
            $from = $this->cacheAccount($raw_data['from']);
            $this->cacheReplies($account, $from, $comment, $reply);
        }
    });
}

private function cachePost(Account $account, Account $from, Post $post) { 
    $post->account()->associate($account);
    $post->from()->associate($from);
    $post->save();
    /* if ($post->isIntractable()) {
        $interaction = Interaction::add($post, $from, $account); 
        Task::add($interaction);
    }*/
 } 

1 个答案:

答案 0 :(得分:0)

模型事件只需要一个参数,即触发该事件的模型。例如:

Post::saved(function(Post $post){
    if ($post->isIntractable()) {
        $interaction = Interaction::add($post, $from, $account); // this will throw error as the variables are not recognized by the event.
        Task::add($interaction);
    }
});

如果您需要传递更多参数,则需要找到一种方法,在触发Post操作之前将这些参数附加到save()模型。