查询数据时,Laravel的belongsTo和'with'出现问题。 我有以下两个名为Leave Type和Leave Application的模型。
1)休假类型
class LeaveType extends Model
{
use SoftDeletes;
public $incrementing = false;
protected $fillable = ['id','company_id','category','renewal_month','no_of_days','status','deleted_at','created_at','updated_at','require_attachment',
'require_reason','employment_type_id','increment_flag','service_duration','increment_days','max_days','carry_forward_flag','carry_forward_value',
'expiry_duration','expiry_flag','advance_notice_required','other_names'];
//hook into model and listen for any eloquent events
public static function boot()
{
parent::boot();
self::creating(
function ($model) {
$model->id = Uuid::uuid4()->toString();
$model->company_id = auth()->user()->company_id;
}
);
}
public function leaveApplication()
{
return $this->hasMany('App\LeaveApplication','leave_type_id','id');
}
}
2)退出申请
class LeaveApplication extends Model
{
const PENDING = '0';
const APPROVED = '1';
const REJECTED = '2';
use SoftDeletes;
protected $fillable = ['id','company_id','user_id','ref_no','applied_date','from_date','to_date','days',
'half_day_flag','leave_type_id','reason','attachment','approved_status','approved_remark','approved_by','is_deducted',
'deleted_at','created_at','updated_at'];
public $incrementing = false;
//hook into model and listen for any eloquent events
public static function boot()
{
parent::boot();
self::creating(
function ($model) {
$model->id = Uuid::uuid4()->toString();
$model->company_id = auth()->user()->company_id;
$model->user_id = auth()->user()->id;
}
);
}
public function leaveType()
{
return $this->belongsTo('App\LeaveType','leave_type_id','id');
}
}
但是当我从表中查询数据
$leaveApplication = LeaveApplication::with('leaveType:id,category')->get();
我得到了这个结果。
{
"id": "5be4af08-9983-4fad-b73d-56f651cd0355",
"company_id": "5410e025-62b1-47db-a65b-8b5e25f6ed59",
"user_id": "dee83d11-5761-4704-8884-cbaa605d9039",
"ref_no": "L20191102",
"applied_date": "2019-11-26",
"from_date": "2019-11-26",
"to_date": "2019-11-27",
"days": 2,
"half_day_flag": 0,
"leave_type_id": "967d1834-061d-4773-b4c8-0d48fe568987",
"reason": null,
"attachment": "/tmp/phpdWQ5oR",
"approved_status": 0,
"approved_remark": null,
"approved_by": null,
"is_deducted": 0,
"deleted_at": null,
"created_at": "2019-11-26 01:37:44",
"updated_at": "2019-11-26 01:37:44",
"leave_type": {
"id": "967d1834-061d-4773-b4c8-0d48fe568987",
"category": "Annual"
}
},
{
"id": "0305a9ae-4d2b-478d-abfc-bbad3030fdf1",
"company_id": "5410e025-62b1-47db-a65b-8b5e25f6ed59",
"user_id": "dee83d11-5761-4704-8884-cbaa605d9039",
"ref_no": "L20191101",
"applied_date": "2019-11-26",
"from_date": "2019-11-26",
"to_date": "2019-11-27",
"days": 2,
"half_day_flag": 0,
"leave_type_id": "8e37b068-42ac-413e-b3f4-8d8c83aa1656",
"reason": null,
"attachment": "/tmp/phpFPsxmQ",
"approved_status": 0,
"approved_remark": null,
"approved_by": null,
"is_deducted": 0,
"deleted_at": null,
"created_at": "2019-11-26 01:36:04",
"updated_at": "2019-11-26 01:36:04",
"leave_type": null
}
LeaveType表中都存在两个休假类型id,但是第二种关系仍然显示休假类型为null。我究竟做错了什么?
更新
LeaveType表的数据如下。
{
"table": "leave_types",
"rows":
[
{
"id": "8e37b068-42ac-413e-b3f4-8d8c83aa1656",
"company_id": "5410e025-62b1-47db-a65b-8b5e25f6ed59",
"category": "Paternity",
"renewal_month": 1,
"no_of_days": 7,
"status": 1,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"require_attachment": 1,
"require_reason": 0,
"employment_type_id": 1,
"increment_flag": 0,
"service_duration": 0,
"increment_days": 0,
"max_days": 0,
"carry_forward_flag": 0,
"carry_forward_value": 1,
"expiry_duration": 12,
"expiry_flag": 0,
"advance_notice_required": 0,
"other_names": null
},
{
"id": "967d1834-061d-4773-b4c8-0d48fe568987",
"company_id": "5410e025-62b1-47db-a65b-8b5e25f6ed59",
"category": "Annual",
"renewal_month": 1,
"no_of_days": 14,
"status": 1,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"require_attachment": 0,
"require_reason": 0,
"employment_type_id": 1,
"increment_flag": 0,
"service_duration": 0,
"increment_days": 0,
"max_days": 0,
"carry_forward_flag": 0,
"carry_forward_value": 1,
"expiry_duration": 12,
"expiry_flag": 0,
"advance_notice_required": 0,
"other_names": null
}
]
}
答案 0 :(得分:0)
在模型中声明正在使用的表。
class LeaveType extends Model
{ ...
protected $table = "leave_type";
public function leaveApplication()
{
return $this->hasMany('App\LeaveApplication','leave_type_id');
}
class LeaveApplication extends Model
{ ...
protected $table = "leave_application ";
public function leaveType()
{
return $this->belongsTo('App\LeaveType','leave_type_id');
}