Php 拉威尔记录关系问题
我在Php 拉威尔记录关系问题,php,database,laravel,laravel-5,eloquent,Php,Database,Laravel,Laravel 5,Eloquent,我在申请人和待处理工作之间有一对多关系,其中我的申请人模型有cnic主键,其中待处理工作有申请人id并将其作为外键指向申请人模型cnic列 申请人型号: protected $primaryKey = 'cnic'; public function pending_jobs() { return $this->hasMany('App\PendingJobs', 'applicant_id'); } public function applicant() { retur
申请人
和待处理工作
之间有一对多关系,其中我的申请人模型有cnic
主键,其中待处理工作
有申请人id
并将其作为外键指向申请人
模型cnic列
申请人型号:
protected $primaryKey = 'cnic';
public function pending_jobs() {
return $this->hasMany('App\PendingJobs', 'applicant_id');
}
public function applicant() {
return $this->belongsTo('App\Applicant', 'applicant_id');
}
待处理工作模式:
protected $primaryKey = 'cnic';
public function pending_jobs() {
return $this->hasMany('App\PendingJobs', 'applicant_id');
}
public function applicant() {
return $this->belongsTo('App\Applicant', 'applicant_id');
}
我正在申请人中插入记录,如:
$req = new \App\Applicant;
$req->full_name = $request->get('full_name');
$req->cnic = $request->get('cnic');
$req->mobile_number = $request->get('mobile_number');
$req->save();
然后在PendingJob
中记录:
$reqr = new \App\PendingJob;
$reqr->applicant_id = $request->get('cnic');
$reqr->job_type = 'residence';
$reqr->status = 'pending';
$reqr->save();
该记录保存并cnic
,它是申请人的主键
,而待处理工作
的申请人id
指向申请人的cnic
也是相同的
但是
关系不加载,从phpmydamin手动插入记录可以正常工作。工作流应为:
$reqr = new \App\PendingJob;
$reqr->job_type = 'residence';
$reqr->status = 'pending';
$req = new \App\Applicant;
$req->full_name = $request->get('full_name');
$req->cnic = $request->get('cnic');
$req->mobile_number = $request->get('mobile_number');
$reqr->applicant()->associate($req);
$reqr->save();
资料来源:
此外,我注意到申请人的主键是cnic
,因此您的关系应根据Laravel文档指定本地密钥
:
App\Application
public function pending_jobs() {
return $this->hasMany('App\PendingJobs', 'applicant_id', 'cnic');
}
App\PendingJob
public function applicant() {
return $this->belongsTo('App\Applicant', 'applicant_id', 'id');
}
您能否共享申请者和待处理职务表的列名?发布迁移。请解释“关系未加载”部分