2016-12-04 181 views
0

我稱爲所有模型帳戶,發佈,評論,任務和交互如何使用應用程序服務提供商爲此

但我有一個錯誤。這是錯誤:

Type error: Argument 1 passed to App\Providers\AppServiceProvider::App\Providers{closure}() must be an instance of App\Account, instance of App\Post given

這是我的代碼:

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); 
    }*/ 
} 

回答

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); 
    } 
}); 

如果您需要更多參數傳遞,你需要找到一種方式觸發save()行動前附加這些參數的Post模型。