2016-12-04 1 views
0

Ich werde alle Modell-Account, Post, Kommentar, Aufgabe und InteraktionSo verwenden Sie App-Service-Provider für diese

aufgerufen, aber ich habe einen Fehler. Dies ist der Fehler:

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

Dies ist mein Code:

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

Antwort

0

Modell Ereignisse erwarten nur einen Parameter, die das Modell, das das Ereignis ausgelöst hat. In Ihrem Fall zum Beispiel:

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

Wenn Sie mehr Parameter zu übergeben, müssen Sie einen Weg finden, diese Parameter an das Post Modell an, bevor die save() Aktion auszulösen.