Laravel Eloquent - relation of relation - 三模型关系

Laravel Eloquent - relation of relation - three model relationship

我正在尝试将以下数据库查询转换为 eloquent 关系。

$device_barcodes = ["BSC0337", "BSC0503", "BSC0626"];
$devices = DB::table('devices')
       ->leftjoin('devices_repairs', 'devices_repairs.device_id', '=', 'devices.device_id')
       ->leftJoin('staff','staff.staff_id','devices_repairs.repair_damaged_by')
       ->whereIn('barcode', $device_barcodes)
       ->get();

到目前为止我有以下内容,但我正在为员工关系而苦苦挣扎。

$devices = Devices::with('repairDetails')->whereIn('barcode', $device_barcodes)->get();

我现在已经用下面的方法克服了这个问题,但这并不理想。

foreach($devices as $device) {
           $staff_name = Staff::find($device->repairDetails->repair_damaged_by);
           $device->staff_name = $staff_name->staff_name;
 }

我希望得到类似的东西:

$devices = Devices::with('repairDetails')->with('staffDetails')->whereIn('barcode', $device_barcodes)->get();

这样我就可以在我的 blade 表格上显示设备详细信息、维修详细信息和员工姓名。

所以基本问题是我试图将三个模型联系在一起。

Devices 是顶级模型,我使用 device_id 主键和外键加入 DevicesRepairs。但是对于 Staff 模型,我需要通过 staff.staff_id.

加入 devices_repairs.repair_damaged

这是我的模型: 设备:

class Devices extends Model
{
    /**
     * @var bool $timestamps Disable timestamps
     */
    public $timestamps = false;
    /**
     * @var string $table Name of the db table
     */
    protected $table = 'devices';
    /**
     * @var string $primaryKey Name of the primary key
     */
    protected $primaryKey = 'device_id';
    /**
     * @var array $fillable The attributes that are mass assignable.
     */
    protected $fillable = [
        'status',
        'order_reference',
        'model',
        'serial',
        'imei',
        'barcode',
        'mobile_number',
        'helpdesk_url_id',
        'device_notes'
    ];

    use Searchable;

    function repairDetails() {
        return $this->hasOne(DevicesRepairs::class, 'device_id');
    }

    function repairHistoryDetails() {
        return $this->hasOne(DevicesRepairsHistory::class, 'device_id');
    }

}

设备维修:

class DevicesRepairs extends Model
{
    use HasFactory;
    protected $table = 'devices_repairs';
    protected $primaryKey = 'device_id';
    public $timestamps = false;
    protected $fillable = [
        'device_id',
        'repair_damanged_by',
        'repair_damage_type',
        'repair_date_received',
        'repair_date_sent_for',
        'repair_damage_notes',
        'repairer_name',
        'repair_is_user_damage',
        'job_number',
        'operator_date_received',
        'operator_date_received_by',
        'operator_date_sent',
        'operator_sent_by',
        'photo_id',
        'photo_id_back'
    ];

    function device() {
        return $this->hasOne(Devices::class, 'device_id');
    }

    //This doesn't work - seems to return a random staff member.
    function staffDetails() {
        return $this->hasOne(Staff::class,'staff_id','repair_damaged_by');
    }  
}

工作人员:

class Staff extends Model
{
    use searchable;

    /**
     * @var bool $timestamps Disable timestamps
     */
    public $timestamps = false;
    /**
     * @var string $table Name of the db table
     */
    protected $table = 'staff';
    /**
     * @var string $primaryKey Name of the primary key
     */
    protected $primaryKey = 'staff_id';
    /**
     * @var array $fillable The attributes that are mass assignable.
     */

    protected $fillable = [
        'staff_id',
        'staff_name',
        'manager_id',
        'is_active',
        'is_mdm_user',
        'user_id',
    ];
}

我的最终目标是能够 return 在我看来这样的字段:

{{$device->barcode}}
{{$device->repairDetails->repair_damage_notes}}

//The above work fine but not this:
{{$device->staffDetails->staff_name}}

将 at 视为枢轴 table,设备与人员之间的多对多关系

class Devices extends Model
{
    public function damagedBy()
    {
        return $this->belongsToMany(Staff::class, 'devices_repairs', 'device_id', 'repair_damaged_by');
    }

所以查询会像

$device_barcodes = ["BSC0337", "BSC0503", "BSC0626"];
$devices = Devices::with('damagedBy')->whereIn('barcode', $device_barcodes)->get();