Laravel 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();
这样我就可以在我的刀片表单上显示设备详细信息、维修详细信息和员工姓名。
所以基本问题是我试图将三个模型联系在一起。
Devices 是顶级模型,我使用 device_id 主键和外键加入 DevicesRepairs。但对于 Staff 模型,我需要将 devices_repairs.repair_damaging by 加入到 Staff.staff_id 中。
这是我的模型: 设备:
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');
}
}
DevicesRepairs:
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',
];
}
我的最终目标是能够返回这样的字段:
{{$device->barcode}}
{{$device->repairDetails->repair_damage_notes}}
//The above work fine but not this:
{{$device->staffDetails->staff_name}}
I am trying to convert the below DB query to an eloquent relation.
$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();
So far I have the below, but I am struggling with the staff relation.
$devices = Devices::with('repairDetails')->whereIn('barcode', $device_barcodes)->get();
I've overcome this for now with the below, but this is not ideal.
foreach($devices as $device) {
$staff_name = Staff::find($device->repairDetails->repair_damaged_by);
$device->staff_name = $staff_name->staff_name;
}
I am hoping for something like:
$devices = Devices::with('repairDetails')->with('staffDetails')->whereIn('barcode', $device_barcodes)->get();
This way I can show the device details, the repair details and the staff name on my blade form.
So the basic issue is I am trying to relate three models together.
Devices is the top model, where I join with DevicesRepairs using the device_id primary and foreign keys. But for the Staff model I need to join devices_repairs.repair_damaged by to staff.staff_id.
Here are my models:
Devices:
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');
}
}
DevicesRepairs:
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');
}
}
Staff:
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',
];
}
My ultimate aim is to be able to return fields like this in my view:
{{$device->barcode}}
{{$device->repairDetails->repair_damage_notes}}
//The above work fine but not this:
{{$device->staffDetails->staff_name}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 at 视为数据透视表,设备和人员之间存在多对多关系,
因此查询将类似于
treat at as a pivot table, a many to many relation between devices and staff
So the query will be like