gpt4 book ai didi

laravel - 如何在laravel eloquent中从多个表中检索数据

转载 作者:行者123 更新时间:2023-12-01 03:22:52 24 4
gpt4 key购买 nike

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'}
],
}

表结构:

语言(`id`,`language_name`,`translate_version`,`is_default`,`status`);
属性(`id`,`required`,`type`,`status`);
attributeetails(id`, `attribute_id`, `language_id`, `attribute_name`, `status`);

最佳答案

尝试这样的事情,

$results =  Attribute::select('id')->with(['attributeDetail.language' => function ($query) {
$query->select('id', 'language_name');
}])->get();

关于laravel - 如何在laravel eloquent中从多个表中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44018746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com