Just released a short video, explaining a topic called . I’ve noticed that it’s quite hard to understand for junior developers, but in reality it’s easy when you see the example.

So, probably the most simple example is , where you may want to have some internal variable to add to your email/SMS notification, but you have to pass them as parameters to the class. That’s a typical example of dependency injection – see how we’re passing $user variable here.

class Hello extends Notification

     function __construct(User $user)
        $this->user = $user;

     function toMail($notifiable)
        return (new MailMessage)
                    ->line('Hi ' . $this->user->name . ' ('.$this->user->email.')')
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our !');

See video below for more explanation.

The post Dependency Injection in Laravel: Notifications Example appeared first on Laravel Daily.

Source link


Please enter your comment!
Please enter your name here