我有这些模型及其相关的数据库表,目前我可以检索所有需求
Requirement::all()
但我只有一个foreigns键列表(destination_id,applier_id,doc_id)。如何直接检索连接到该foreigns键的行?
class Requirement extends Model
{
protected $fillable = [
'required',
'destination_id',
'applier_id',
'doc_id'
];
public function destination()
{
return $this->belongsTo(Destination::class);
}
public function applier()
{
return $this->belongsTo(Applier::class);
}
public function doc()
{
return $this->belongsTo(Doc::class);
}
}
class Doc extends Model
{
protected $fillable = [
'type',
'description',
'note'
];
public function requirements()
{
return $this->hasMany(Requirement::class);
}
}
class Destination extends Model
{
protected $fillable = [
'country',
'passying_country',
'transfer_conditions',
'passing_conditions'
];
public function requirements()
{
return $this->hasMany(Requirement::class);
}
}
答案 0 :(得分:1)
您可以拨打with()
功能而不是all()
。所以如果你尝试以下这个:
$requirements = Requirement::with('destination', 'applier', 'doc')->get();
将其设为dd($requirements)
并查看输出。
希望它会奏效。