[英]One to One relationShip in laravel
Job Model 工作模式
class Job_Model extends Model
{
protected $table = "tbljobs";
protected $primaryKey = "JobID";
public $timestamps = false;
public function OrderType()
{
return $this->hasOne('\App\Models\OrderType_Model', "OrderTypeID");
}
}
Order Type Model 订单类型模型
class OrderType_Model extends Model
{
protected $table = "tblOrderType";
protected $primaryKey = "OrderTypeID";
public $timestamps = false;
}
Query 询问
$Jobs = \App\Models\Job_Model::with("OrderType")->get();
I am retrieving all records from tbljobs
table. 我正在从tbljobs
表中检索所有记录。 but each record is not showing associated record from Order Type Table. 但是每个记录未显示“订单类型表”中的关联记录。
Am I missing something ? 我想念什么吗?
dd() result dd()结果
You need has()
and with()
. 您需要has()
和with()
。 Has returns only jobs with order types and with actually injects the order type into each job. 仅返回具有订单类型的作业,并且实际将订单类型注入每个作业中。
$Jobs = \App\Models\Job_Model::has("OrderType")->with("OrderType")->get();
Edit: if your foreign key is actually in the Jobs table, not the Order type table, you want a belongs to relationship. 编辑:如果您的外键实际上在Jobs表中,而不在Order type表中,则您想要一个属于关系。
hasOne()
assumes the foreign key is in the other table. hasOne()
假定外键在另一个表中。
belongsTo()
assumes the foreign key is in this table. belongsTo()
假设外键在此表中。
I suppose your question is just how you could see dumped data on the screen? 我想您的问题是,如何才能在屏幕上看到转储的数据? Try it like this 像这样尝试
$Jobs = \App\Models\Job_Model::with("OrderType")->get();
foreach ($Jobs as $job) {
var_dump($job->toArray());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.