Skip to content
Advertisement

Trying to get property ‘approval_code’ of non-object

I’m trying to build a task scheduler for sending email routine and i got this error when i try to get approval_code from auth.

Here are my code in mail:

<?php

namespace AppMail;


use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesDB;
use AppServicesGetApprovalPersonByRouteCode;
use AppHabisKontrak;

class HabisKontrakMail extends Mailable
{
    use Queueable, SerializesModels;
    
    /**
     * Create a new message instance.
     * @param HabisKontrak $habisKontrak
     * @param [type] $person
     * @param [type] $justInfo
     *
     */
    public function __construct()
    {
        //
        // $this->link = $link;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $reminder_kontrak=HabisKontrak::all();
        $person = GetApprovalPersonByRouteCode::getPerson(auth()->user()->approval_code,HabisKontrak::KODE_MODUL);
        return $this->subject('Pemberitahuan Karyawan Habis Kontrak')
                    ->view('mail.reminder.habisKontrak')
                    ->with([
                        'reminder_kontrak' => $reminder_kontrak,
                        'person' => $person
                    ]);
        // }
    }
}

and here are code for the GetApprovalPersonByRouteCode

<?php

namespace AppServices;

use DB;
use AppPermintaanKendaraan;

class GetApprovalPersonByRouteCode{
    /**
     * Get person approval / just info
     *
     * @param string $approvalCode
     * @param string $kode_modul
     * @return void
     */
    public static function getPerson($approvalCode, $kode_modul){
       return DB::table('approvalroutedetail')
        ->select([
            'approvalroutedetail.nik',
            'approvalroutedetail.sequence',
            'approvalroutedetail.just_info',
            'approvalroutedetail.modul',
            'approvalroutedetail.approvalroute_id',
            'karyawan.nama',
            'karyawan.departemensubsub',
            'karyawan.email',
            'karyawan.jabatan'
        ])->join('karyawan','approvalroutedetail.nik','karyawan.nik')
        ->where('approvalroutedetail.modul', $kode_modul)
        ->where('approvalroutedetail.approvalroute_id', $approvalCode)
        ->orderBy('approvalroutedetail.sequence','asc')
        ->orderBy('approvalroutedetail.just_info','asc')
        ->get();
    }
}

I tried to pass the data via constructor like this

<?php

namespace AppMail;


use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesDB;
use AppServicesGetApprovalPersonByRouteCode;
use AppHabisKontrak;
use AppReminder;
use DateTime;

class HabisKontrakMail extends Mailable
{
    use Queueable, SerializesModels;
    
    protected $person; //approver (collection: GetApprovalPersonByRouteCode::getPerson)
    
    /**
     * Create a new message instance.
     * @param HabisKontrak $habisKontrak
     * @param [type] $person
     * @param [type] $justInfo
     *
     */
    public function __construct($person)
    {
        //
        $this->person = $person;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $reminder_kontrak=HabisKontrak::all();
        return $this->subject('Pemberitahuan Karyawan Habis Kontrak')
                    ->view('mail.reminder.habisKontrak')
                    ->with([
                        'reminder_kontrak' => $reminder_kontrak,
                        'person' => $this->person
                    ]);
        // }
    }
}

And then i got the error massage

Too few arguments to function AppMailHabisKontrakMail::__construct(), 0 passed

I try to get this value getPerson(auth()->user()->approval_code,HabisKontrak::KODE_MODUL), Thankyou.

Advertisement

Answer

Task scheduler does not have a user session, thus auth()->user() can only return null (non-object).

To fix this, your crontab command can provide the user argument along. And your command can get a user with arguments:

Arguments

All user supplied arguments and options are wrapped in curly braces. In the following example, the command defines one required argument: user:

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'mail:send {user}';

You may also make arguments optional or define default values for arguments:

// Optional argument...
mail:send {user?}

// Optional argument with default value...
mail:send {user=foo}

Simply pass the load a User object with the user argument (probably the user id or the user name). Pass this User on to HabisKontrakMail object, then you’re good to go.

Wait. Doesn’t that mean the same user is sending every time?

One catch is you can only have 1 user sending all the notification email. But according to the code you showed, the database don’t seem to contain which user actually initiated the notification.

If you need to actually check it, you’ll have to store the user id / username for the notification before it is scheduled. And I don’t have enough information to suggest anything.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement