I'm trying to build a task scheduler for sending email routine and i got this error when i try to getapproval_code
from auth.
Here are my code in mail:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use App\Services\GetApprovalPersonByRouteCode;
use App\HabisKontrak;
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 theGetApprovalPersonByRouteCode
<?php
namespace App\Services;
use DB;
use App\PermintaanKendaraan;
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 App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use App\Services\GetApprovalPersonByRouteCode;
use App\HabisKontrak;
use App\Reminder;
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 App\Mail\HabisKontrakMail::__construct(), 0 passed
I try to get this valuegetPerson(auth()->user()->approval_code,HabisKontrak::KODE_MODUL)
, Thankyou.
Task scheduler does not have a user session, thusauth()->user()
can only returnnull
(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 aUser
object with theuser
argument (probably the user id or the user name). Pass thisUser
on toHabisKontrakMail
object, then you're good to go.
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.