Search code examples
phplaravelnotificationsjobs

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


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

Solution

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