首页 > 解决方案 > 试图获取非对象的属性“approval_code”

问题描述

我正在尝试构建一个用于发送电子邮件例程的任务调度程序,当我尝试approval_code从 auth 获取时出现此错误。

这是我在邮件中的代码:

<?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
                    ]);
        // }
    }
}

这里是代码GetApprovalPersonByRouteCode

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

我试图通过这样的构造函数传递数据

<?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
                    ]);
        // }
    }
}

然后我得到了错误按摩

函数 App\Mail\HabisKontrakMail::__construct() 的参数太少,通过了 0

我试着得到这个值getPerson(auth()->user()->approval_code,HabisKontrak::KODE_MODUL),谢谢。

标签: phplaraveleloquent

解决方案


任务调度程序没有用户会话,因此auth()->user()只能返回null(非对象)。

要解决此问题,您的 crontab 命令可以提供 user 参数。你的命令可以得到一个带参数的用户:

论据

所有用户提供的参数和选项都包含在花括号中。在以下示例中,该命令定义了一个必需参数user

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

您还可以将参数设为可选或为参数定义默认值:

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

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

只需传递带有参数的加载User对象user(可能是用户 ID 或用户名)。将此传递UserHabisKontrakMail对象,然后您就可以开始了。

等待。这不意味着每次发送的都是同一个用户吗?

一个问题是您只能让 1 个用户发送所有通知电子邮件。但是根据您显示的代码,数据库似乎不包含实际启动通知的用户。

如果您需要实际检查它,则必须在安排通知之前存储用户 ID/用户名。而且我没有足够的信息来提出任何建议。


推荐阅读