mark
mark

Reputation: 121

laravel 5.6 Undefined variable: user when trying to send email

I am trying to send an email using Notification class in laravel 5.6 which I am new to.

I am trying to pass user and book information but every time I get the following :

Undefined variable: user

here is my successEmail.php:

  <?php

namespace BOOK_DONATION\Notifications;

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

class successEmail extends Notification
{
    use Queueable;
    public $user;
    public $book;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($user,$book)
    {
        //
        $this->user=$user;
        $this->book=$book;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
        ->greeting('Dear'.$user->name.'thak you for creating donation for '.$book->name);    
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

and here is the function that call the class successEmail:

 public function doBook(Request $request){
            $validatedData = $request->validate([
                'title' => 'string|required|max:255',
                'Author'=> 'string|required|max:255',
                'Cover_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
                'Book_description'=>'string|required|max:255',

            ]);
        $path= $request->Cover_image->store('images');
        $uid=Auth::user()->id;
        $country=Auth::user()->country;
        $city=Auth::user()->city;
        $book=Book::create(['title'=>$request->input('title'),
                       'Author'=>$request->input('Author'),
                       'user_id'=>$uid,
                       'country'=>$country,
                        'city' =>$city,
                        'Book_description'=>$request->input('Book_description'),
                        'path'=>$path,
                        ]);
         $user = Auth::user();

         $user->notify(new successEmail($user,$book));

        return redirect('/donation/create')->with('status', 'Thank you for your donation');
    }

as you see I passed the user and the book variable to the constructor and declared them as public so what is that I am missing?

Upvotes: 0

Views: 702

Answers (4)

JHOAN B. HENRICHE
JHOAN B. HENRICHE

Reputation: 409

That happened to me too. In the function toMail you need to redeclare the variables like this

$user= $this->user;

And then you call the variable like this

  ->line('user:'.''.$user,'')

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try this:

use Auth;

public function toMail($notifiable)
    {

        return (new MailMessage)
        ->greeting('Dear'.$this->user->name.'thak you for creating donation for '.$this->book->name);    
    }

Upvotes: 1

James Cook
James Cook

Reputation: 217

You need to make the call to $this->user to access the variable in the class scope.

 /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
        ->greeting('Dear'.$this->user->name.'thak you for creating donation for '.$this->book->name);    
    }

Upvotes: 5

user1669496
user1669496

Reputation: 33058

Those variables do not exist in the function scope but they do exist as class properties so they should be treated as such.

return (new MailMessage)
    ->greeting('Dear '.$this->user->name.' thank you for creating donation for '.$this->book->name);

Upvotes: 7

Related Questions