Pass Variable From Controller To Notification In Laravel

- 1 answer

I am finding it hard to understand the examples from the docs to the scenario I am having. In my project I have an application form which filled up by the user then admin will update that form once the application is approved, canceled etc.

Now I want to notify the user that her/his application has been approved, canceled etc.

in my controller:

public function update(Request $request, $id)
    $this->validate($request, [
        'status' => 'required'
    $requestData = $request->all();

    $loanapplication = LoanApplication::findOrFail($id);

        "status"        => $request->status,
        "admin_notes"   => $request->admin_notes,
        "date_approval" => $request->date_approved

    if($request->notifyBorrower = 'on') {

        $user_id = $loanapplication->user_id;
        $status = $request->status;
        $this->notify(new AdminResponseToApplication($user_id));   

    return redirect()->back()->with('flash_message', 'LoanApplication updated!');

In my AdminResponseToApplication.php I like to achieve this

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class AdminResponseToApplication extends Notification implements ShouldQueue
    use Queueable;  
    public function __construct()

    public function via($notifiable)
        return ['mail','database'];

    public function toMail($notifiable)
        return (new MailMessage)
            ->line(.$user->nameHere. 'your application has been '.$statusHere.'.')
            ->action('check it out', url('/'))
            ->subject('Regarding with your loan application')
            ->line('This is system generated. Do not reply here.');

    public function toDatabase($notifiable)
        return [
            'user_id' => $user->nameHere,
            'status' => $statusHere,
            'title' => .$user->nameHere. 'your application has been '.$statusHere.'.',
            'url' => '/'

How can I achieve that? Thank you in advance!



Get user object and call function notify() on it. $this->notify() will not work because $this is not an instance of User class.

$user = User::find($user_id);

$user in the $user->notify(new AdminResponseToApplication($data)) function is available in notification class as $notifiable.

You can get any value of that object using $notifiable->name etc.


AdminResponseToApplication is a class and you can do anything with it that a php class can.

So you can pass as many variables as you want to AdminResponseToApplication class in constructor and do what you want.

$user->notify(new AdminResponseToApplication($data))

As shown above I am sending a $data object to the class which is available in the constructor.

In the class

class AdminResponseToApplication extends notification implements ShouldQueue{
    use Queueable;

    public $myData;
    public function __construct($data)
        $this->myData = $data;  //now you have a $data copied to $this->myData which
        // you can call it using $this->myData in any function of this class.