简体   繁体   English

Laravel 5.3 Eloquent-从3表中选择

[英]Laravel 5.3 Eloquent - select from 3 tables

in addition to my last question Laravel 5.3 Eloquent Relationships 除了我的最后一个问题, Laravel 5.3雄辩的关系

i added a new table called Labels and of course create a new model for this table. 我添加了一个名为Labels的新表,当然也为此表创建了一个新模型。

Languages 语言能力

id
name
short_name

Dictionary 字典

id
label_id
value
language_id

Labels 标签

id
label_name

and i have this code: 我有这个代码:

$lables = Dictionary::whereHas('language', function($query) {
        $short_name = basename(__DIR__);
        $query->where('short_name', $short_name);
    })->pluck('value', 'label_id')->toArray();

I want to pull out the label_name field instead of the label_id 我想拉出label_name字段而不是label_id

but I dont know how to make this join. 但我不知道该如何加入。

You may try using join() as 您可以尝试使用join()作为

$lables = Dictionary::whereHas('language', function($query) {
                    $query->where('short_name', 'en');
                })
                ->join('labels', 'dictionary.label_id', '=', 'labels.id')
                ->pluck('dictionary.value', 'labels.label_name')
                ->toArray();

I am not 100% sure that will work, but you can give it a shot. 我不确定100%会奏效,但您可以试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM