laravel 5.6 Undefined variable: user when trying to send email - php

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?

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

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

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

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,'')

Related

Argument 1 passed to App\Notifications\NewInstalasi::__construct() must be an instance of App\Models\User, instance of App\Models\Instalasi given

i am doing project with notification real time use pusher in laravel 8, and then i have got this error "Argument 1 passed to App\Notifications\NewInstalasi::__construct() must be an instance of App\Models\User, instance of App\Models\Instalasi given, called in C:\xampp\htdocs\rsud\laporan_teknisi_improvement\app\Observers\InstalasiObserver.php on line 15".
in my Notification/NewInstalasi.php codes :
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Models\User;
use App\Models\Instalasi;
class NewInstalasi extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
protected $user, $instalasi;
public function __construct(User $users, Instalasi $instalasi)
{
$this->instalasi = $instalasi;
$this->users = $users;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database', 'broadcast'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
'user_id' => $this->users->id,
'user_name' => $this->users->name,
'instalasi' => $this->instalasi->nama,
];
}
}
and in Observer/InstalasiObserver.php codes :
<?php
namespace App\Observers;
use App\Notifications\NewInstalasi;
use App\Models\User;
use App\Models\Instalasi;
class InstalasiObserver
{
public function created(Instalasi $instalasi)
{
$author = $instalasi->user;
$users = User::all();
foreach ($users as $user) {
$user->notify(new NewInstalasi($instalasi,$author));
}
}
}

Laravel - How to add redis rate limit with queued notification?

I tried to set rate limit for my notifications queue (called email)
Here is the documentation I checked here. I use laravel 5.6.
Here is the code I tried
class MyController extends Controller{
function myControllerFunction(){
Redis::throttle('email')->allow(1)->every(120)->then(function () use ($user, $userReport) {
$user->notify(new ReportStandard($userReport));
}, function ($error) {
Log::error($error);
return $this->release(120);
});
}
}
I got an error on $this->release(120) because the $this in the closure refer to the MyController class instead of a Job class.
I do not know how I can rate limit my email queue used by notifications?
Notes: ReportStandard is defined this way
class ReportStandard extends Notification implements ShouldQueue
{
use Queueable;
public $userReport;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($userReport)
{
$this->queue = "email";
$this->userReport = $userReport;
}
/**
* 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)
{
Log::info(now() . " report-standard");
return (new MailMessage)
->subject('Reçu pour votre paiement à Autorigin')
->view('front.mails.cross-sell.report-standard', [
'notifiable' => $notifiable,
'userReport' => $this->userReport
]);
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

email notification in laravel 5.5 i get this error ```Trying to access array offset on value of type null```

i want to get email notification each time a new user is registered but after creating php artisan make:notification Taskcompleted and added Notification::route('mail','admin#gmail.com')->notify(new TaskCompleted()); like this in my contoller
public function store(Request $request){
$employee = request()->validate([
'employee_id' => 'required|max:250',
'name' => 'required|max:100',
'place_of_birth' => 'nullable|max:100',]);
Notification::route('mail','admin#gmail.com')->notify(new TaskCompleted());
i keep getting this error
Trying to access array offset on value of type null i have imported the necessary class and configured my .env file with mailtrap,still yet same error
taskcompleted file
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class TaskCompleted extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* 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)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
ok, I got the solution I was using PHP 7.4 for laravel 5.5 so I downgraded it to PHP 7.2 works fine now

How t solve Call to undefined method stdClass::notify()

laravel notify() undefined method. how to solved it....help me..
Error is:-
Call to undefined method stdClass::notify()
My Controller Code Here:
use Illuminate\Http\Request;
use \App\Notifications\ResetLink;
use Auth;
use App\User;
use DB;
use App\Password;
public function forgot(){
return view('forgot');
}
public function forgotPw(Request $request){
if($user = User::where('email',$request->email)->first()){
DB::table('password_resets')->insert([
'token' => $this->token(),
'email' => $user->email
]);
$pr = DB::table('password_resets')->where('email',$user->email)- >first();
$pr->notify(new ResetLink($pr));
request()->session()->flash('success', "Forgot Link Successfully Sent...");
return redirect('login');
} else {
request()->session()->flash('error', "Forgot Link Not Sent...");
return redirect('forgot');
}
}
My Notification Code Here:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ResetLink extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct()
{
public $pr;
}
/**
* 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)
->line('Click the Button and Reset Password!')
->action('Password Reset', url('/reset'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
I tried to last 2 hours but No send mail in mailtrap....
Thank You Advance for Help me..

Laravel 5.7 Multilogin SendEmailVerificationNotification error

I'm new to Laravel. I just created a custom login with laravel 5.7. When I tried to reset password I'm getting this error:
"Declaration of
App\Employee::sendEmailVerificationNotification($token) should be
compatible with
Illuminate\Foundation\Auth\User::sendEmailVerificationNotification()"
Does anyone know how to resolve this error?
You may do something like this
class Employee extends Model implements MustVerifyEmail {
public function sendEmailVerificationNotification()
{
$this->notify(new VerifyEmail);
}
}
if you want to call it like Employee::sendEmailVerificationNotification() and if you want to verify the token you should extend the VerifyEmail notification something like
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Lang;
use Illuminate\Auth\Notifications\VerifyEmail;
class VerifyEmailNotification extends VerifyEmail
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($token)
{
//verify token
}
/**
* 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)
{
if (static::$toMailCallback) {
return call_user_func(static::$toMailCallback, $notifiable);
}
return (new MailMessage)
->subject(Lang::getFromJson('Verify Email Address'))
->line(Lang::getFromJson('Please click the button below to verify your email address'))
->action(
Lang::getFromJson('Verify Email Address'),
$this->verificationUrl($notifiable)
)
->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Then in Employee model
public function sendEmailVerificationNotification($token)
{
$this->notify(new VerifyEmailNotification($token)); // your custom notification
}
You have to follow the same method signature if you want to override it-
You are overriding this method-
Illuminate\Foundation\Auth\User::sendEmailVerificationNotification()
to this-
App\Employee::sendEmailVerificationNotification($token)
If you notice the difference, you have passed $token in the method while the original method definition does not support that.
Create a different method if you need a different signature from the original method.

Categories