如何从 laravel eloquent 中的多个 table 检索数据
how to retrive data from multiple table in laravel eloquent
Model relation
---------------------
language.php
----
public function attributeDetail()
{
return $this->hasMany(AttributeDetail::class, 'language_id');
}
attribute.php
----
public function attributeDetail()
{
return $this->hasMany(AttributeDetail::class, 'attribute_id');
}
attributeDetail.php
----
public function language()
{
return $this->belongsTo(Language::class);
}
public function attribute()
{
return $this->belongsTo(Attribute::class);
}
我想像这样显示 json 对象
{
'attribute_id' => 101,
'available_language' => [
{'id' => 1,'language_name' => 'English'},
{'id' => 2,'language_name' => 'French'}
],
}
table结构:
languages(`id`, `language_name`, `translate_version`, `is_default`, `status`);
attributes(`id`, `required`, `type`, `status`);
attributedetails(id`, `attribute_id`, `language_id`, `attribute_name`, `status`);
尝试这样的事情,
$results = Attribute::select('id')->with(['attributeDetail.language' => function ($query) {
$query->select('id', 'language_name');
}])->get();
Model relation
---------------------
language.php
----
public function attributeDetail()
{
return $this->hasMany(AttributeDetail::class, 'language_id');
}
attribute.php
----
public function attributeDetail()
{
return $this->hasMany(AttributeDetail::class, 'attribute_id');
}
attributeDetail.php
----
public function language()
{
return $this->belongsTo(Language::class);
}
public function attribute()
{
return $this->belongsTo(Attribute::class);
}
我想像这样显示 json 对象
{
'attribute_id' => 101,
'available_language' => [
{'id' => 1,'language_name' => 'English'},
{'id' => 2,'language_name' => 'French'}
],
}
table结构:
languages(`id`, `language_name`, `translate_version`, `is_default`, `status`); attributes(`id`, `required`, `type`, `status`); attributedetails(id`, `attribute_id`, `language_id`, `attribute_name`, `status`);
尝试这样的事情,
$results = Attribute::select('id')->with(['attributeDetail.language' => function ($query) {
$query->select('id', 'language_name');
}])->get();