首先我想道歉,如果它是一個重複或重複的問題。Laravel依賴注入,使用設置功能
我是依賴注入的「新」。 我明白我是如何在__constructor函數中做到的,但不確定setter函數。 我想正確使用DI,問題是:我使用的正確方法在下面的代碼:
public function setNotification(Notification $notification)
{
$this->notification = $notification;
}
public function handle(PostWasCommented $event)
{
$post = $event->comment->post;
$post->load('blog');
$followers = $post->followers;
$online_ids = Redis::pipeline(function($pipe) use ($event, $followers, $post)
{
foreach($followers as $follower){
if(Auth::id() != $follower->user_id){
$this->setNotification(new Notification());
$this->notification->from_id = Auth::id();
$this->notification->to_id = $follower->user_id;
$this->notification->type = 'PostComment';
$this->notification->source_id = $event->comment->id;
$this->notification->parent_id = $event->comment->post_id;
$this->notification->blog_name = $post->blog->link_name;
if($post->user_id == $follower->user_id)
$this->notification->my_post = true;
else
$this->notification->my_post = false;
$this->notification->save();
$pipe->get('user'.$follower->user_id);
}
}
});
我發現使用質量分配可以能幫助我:)
我不確定在這裏看到依賴注入。 _may_限定的唯一的東西是PostWasCommented的論點,但我不認爲這就是你要求的。 –