Call to a member function notify() on null in on jobs - php

so i have this variables in NasabahEloquent.php for my notifications.
$shohibuls = ShohibulFinance::where('barang_id','=',$submission->id)->get();
the notifications:
foreach($shohibuls as $sohib){
User::find($sohib->shohibul_id)->notify(New NasabahAkadItemToInvestor($submission,$data));
}
the notifications runs fine. but when in put it in a jobs for sending email, it return an error :
Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function notify() on null in /opt/lampp/htdocs/_pain/app/Jobs/JobForNasabahAkadItemToInvestor.php:36
this is the job dispatch on NasabahEloquent.php
foreach($shohibuls as $sohib){
$userdata = User::where('id',$sohib->shohibul_id)->get();
$AkadEndJob = (new JobForNasabahAkadItemToInvestor($userdata,$submission))->delay(Carbon::now()->addSeconds(2));
dispatch($AkadEndJob);
}
this is the handle and line 36 where the error comes from in JobForNasabahAkadItemToInvestor.php
public function handle()
{
$this->userdata->notify(New NasabahAkadItemToInvestorMail($this->submission));
}
why does my job have null on the user variables?
EDIT : the JobForNasabahAkadItemToInvestor.php file
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Notifications\NasabahAkadItemToInvestorMail;
class JobForNasabahAkadItemToInvestor implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $userdata;
private $submission;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($userdata, $submission)
{
$this->$userdata = $userdata;
$this->$submission = $submission;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
//nda tau lagi dah
//FIXME:Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function notify() on null in /opt/lampp/htdocs/_pain/app/Jobs/JobForNasabahAkadItemToInvestor.php:36
$this->userdata->notify(New NasabahAkadItemToInvestorMail($this->submission));
}
}

In your __construct, you access the properties the wrong way.
$this->$userdata = $userdata;
$this->$submission = $submission;
Should be:
$this->userdata = $userdata;
$this->submission = $submission;
Notice the excess $ signs. I think that's what causes the problem.

Related

RateLimited middleware not working in Laravel 9 Job

I'm trying to send email notifications to my app users, limiting the number of emails to only 2 per minute, so as not to breach my email provider's allowed sending rate. So in MailingController.php I have:
<?php
namespace App\Http\Controllers;
use App\Jobs\SendMessages;
use Illuminate\Http\Request;
class MailingController extends Controller
{
public function send(Request $request) {
SendMessages::dispatch();
return redirect()->back()->withSuccess('Successful operation.');
}
}
SendMessages.php content is:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Jobs\Middleware\RateLimited;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Redis;
class SendMessages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 7200;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$users = User::all();
foreach ($users as $user) {
Log::debug('SendMessages#handle() ' . $user->email);
$user->notify(new VerifyEmail);
}
}
public function middleware()
{
return [new RateLimited];
}
}
And finally, RateLimited.php content is:
<?php
namespace App\Jobs\Middleware;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class RateLimited
{
/**
* Process the queued job.
*
* #param mixed $job
* #param callable $next
* #return mixed
*/
public function handle($job, $next)
{
Redis::throttle('key')
->block(0)->allow(1)->every(30)
->then(function () use ($job, $next) {
// Lock obtained...
Log::debug('RateLimited');
$next($job);
}, function () use ($job) {
// Could not obtain lock...
$job->release(16);
});
}
}
All these are copied and pasted from a Laravel 8 application that works perfectly fine, sending one email every 30 seconds as expected, but in my brand new Laravel 9 application the same code no longer works, and all email notifications are sent at once. I know I must be missing something, but have ran out of ideas. Any help would be greatly appreciated.
Thank you.

laravel queue job , Dispatch two argument

this is my controller
SmsAdsJob::dispatch('aaa','bbb');
this is my job
protected $request;
public function __construct($request)
{
$this->request = $request;
}
public function handle()
{
$clients=$this->request;
logger($clients);
}
this is logger
local.DEBUG: aaa
Problem is in controller I dispatch two value but in job I get One value,But I should get two value in job, can you give some advise?
You Can pass your params in an array or object datatypes. For instance:
$params = ['aaa', 'bbb']
SmsAdsJob::dispatch( $params );
Or:
public function __construct($param1, $param2)
{
$this->param1 = $param1;
$this->param2 = $param2;
}
When you pass to variables you should consider that your job class must know how many params do you want to receive.
SmsAdsJob should have two properties in It's constructor. Then you can access them. I don't understand why you are using "request" in the Job
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SmsAdsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $a;
private $b;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($a,$b)
{
//
$this->a = $a;
$this->b = $b;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
echo $this->a,PHP_EOL;
echo $this->b,PHP_EOL;
}
}
Please check https://laravel.com/docs/8.x/queues

Error with Laravel and FFMpeg in queue job

I'm using Laravel with the protonemedia/laravel-ffmpeg package https://github.com/protonemedia/laravel-ffmpeg
I create a Job to handle video conversion and concatenation and this error appears:
TypeError: Argument 1 passed to App\Jobs\VideoConversion::App\Jobs{closure}() must be an instance of FFMpeg\Filters\Video\VideoFilters, instance of FFMpeg\Filters\AdvancedMedia\ComplexFilters given in /Users/fab/Sites/add-intro/app/Jobs/VideoConversion.php:43
Here is the code of the Job:
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Video;
use Storage, Str, ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
use FFMpeg\Filters\Video\VideoFilters;
class VideoConversion implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $video;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Video $video)
{
$this->video = $video;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$filename = Str::uuid().'.mp4';
FFMpeg::fromDisk('public')
->open(['intro.mp4', $this->video->path])
->addFilter(function (VideoFilters $filters) {
$filters->resize(new \FFMpeg\Coordinate\Dimension(640, 480));
})
->export()
->toDisk('public')
->inFormat(new \FFMpeg\Format\Video\X264)
->concatWithTranscoding($hasVideo = true, $hasAudio = true)
->save($filename);
}
public function failed(Throwable $exception)
{
FFMpeg::cleanupTemporaryFiles();
dd($exception);
}
}
What I'm doing wrong ?
Thanks for help

Emailing in Laravel

I am trying to send a email in Laravel. It works finicky to say at least. got couple of classes for sending email for different conditions. Only difference would be markup of sent email and blade file which data is sent to before sending. I tried using use Mail; in controller from whom i'm sending it, with \Mail::(... and use Illuminate\Support\Facades\Mail; with Mail::(... and vice-versa. So it ain't it.
Here are the mail files:
ToHR file:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class toHR extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.toHR');
}
}
ToHRFirst file:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class toHRFirst extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.toHRFirst');
}
}
And my test file JobCreated:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class JobCreated extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.job-created');
}
}
They'll all the same, yet error occurs. What gives? Did i miss something? Any help will be appreciated, just point me in the right direction.
Code which i use to send email:
Variables used get values from queries which are rather long, but they are not an issue anyways.
Ver1:
Mail::to($user->email)->send(
new JobCreated($user,$job,$jobCount)
);
Ver2:
Mail::to($user->email)->send(
new toHRFirst($user,$job,$jobCount)
);
Ver3:
Mail::to($user->email)->send(
new toHR($user,$job,$jobCount)
);
Sending works if it's used first version. Other cannot be found and that's the error.
Try renaming your files to follow Laravel convention;
You can see that the file JobCreated has a class JobCreated. You need to respect the uppercases:
Rename the ToHR.php file into ToHr.php, then:
Rename the class class toHRinto class ToHr
Rename the ToHRFirst.php file into ToHrfirst.php, then:
Rename the class class toHRFirst into class ToHrfirst
Or if you want to keep the uppercases, just rename the classes like so:
class toHRinto class ToHR
class toHRFirstinto class ToHRFirst
Finally, don't forget to import your job class at the top of your controller file (or wherever you use the Mail method:
use App\Mail\ToHR;
and
use App\Mail\ToHRFirst;

Trying to get property of non-object in Job Laravel

I'm trying to create job to register users and tried to follow Jeffrey's video but looks like dispatchfrom is removed for some reason. This is what i'm trying to do now:
This is my controller:
public function PostSignUp(Request $request)
{
dispatch(new RegisterUser($request->all()));
return 'done';
}
This is my job:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class RegisterUser implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $request;
/**
* Create a new job instance.
* #param $request
* #return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = $this->request->email;
var_dump('I should register user with email:' . $email);
}
}
I also tried to put
just $request instead of $request->all()
but then i get
Serialization of 'Closure' is not allowed
And now I'm getting Trying to get property of non-object error. Is this good way to pass whole request to job ? Should i do it some other way ?
try with input()
$request->input()
When you do $request->all(), that means you are passing an array to the job and not the whole $request. Therefore, you can simply do this in your Job Handler.
public function handle()
{
$email = $this->request['email'];
var_dump('I should register user with email:' . $email);
}
Let me know if something else is required :)

Categories