0

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

    $loanapplication->update([
        "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

<?php
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!

0

1 Answer 1

2

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.

Remember:

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.
    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks @Afraz. it has error, it said "Undefined variable: data". I am bit lost on this part $user->notify(new AdminResponseToApplication($data)); , I know this is just your example, but what does $data should have? because what I am trying to pass is to data from 2 different table, one is the user and the other one is status from status table.
Do not pass $user to constructor because $user->notify() is already passing it as $notifiable. Only send $status. Hope this will remove the error you facing. $user->notify(new NotificationClassName($status));
error is gone but its not firing the notification. currently I have now this on my update function of my controller, $user->notify(new AdminResponseToApplication($statusName)); on AdminResponseToApplication.php i declared public $myStatus; and 'public function __construct($statusName) {$this->myStatus = $statusName; }' in public function toDatabase($notifiable) i return 'title' => $notifiable->name.' your loan has been '.$statusName->name,
The code looks ok to me. Make sure your notifications requirements are done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.