Laravel 5.3雄辩的关系 [英] Laravel 5.3 Eloquent Relationships
问题描述
我正在尝试从数据库中选择lang文件的值,
我创建了一个文件,并将其命名为global.php
,并在文件中我尝试这样做:
使用App \Dictionary;
$ language_id = 1;
$ dictionary = array();
$ lables = Dictionary :: where('language_id',$ language_id) - > get();
foreach($ lables as $ label):
$ dictionary [$ label-> label] = $ label-> value;
endforeach;
return $ dictionary;
现在,这是正常工作,但我想使用short_name字段选择行,而不是id语言
我想要这样做:
$ lables = Dictionary :: all() - > language() - > where('short_name','en') - > get();
我的数据库如下所示:
语言
id
name //例如:英文
short_name / / for exmaple:en
字典
id
key
value
language_id
,我的模型如下所示:
语言模型
命名空间应用程序;
使用Illuminate\Database\Eloquent\Model;
使用Illuminate\Database\Eloquent\SoftDeletes;
类语言扩展模型
{
使用SoftDeletes;
protected $ softDelete = true;
protected $ dates = ['deleted_at'];
public function dictionary()
{
return $ this-> hasMany('App \Dictionary');
}
}
字典模型 p>
<?php
命名空间应用程序;
使用Illuminate\Database\Eloquent\Model;
类字典扩展模型
{
protected $ table ='dictionary'
public function language()
{
return $ this-> belongsTo('App \Language');
}
}
感谢您的帮助! / p>
!!! 更新 !!!
我添加了一个名为 / p>
标签
id
label_name
并将字典表更改为:
字典
id
lable_id
value
language_id
我该如何做这项工作,所以我可以拉标签名而不是label_id
$ lables = Dictionary :: whereHas('language',function($ query){
$ short_name = basename(__ DIR__);
$ query-> where('short_name',$ short_name);
}) - > pluck('value','label_id') - > toArray()你可以使用Laravel的,其中Has ()
函数为: $ lables = Dictionary :: whereHas('language',function $ query){
$ query-> where('short_name','en');
}) - > get();
更新
如果你想摆脱你的 foreach()
,那么你可以使用Laravel的 pluck()
函数:
$ lables = Dictionary :: whereHas('language',function($ query){
$ query-> ;其中('short_name','en');
}) - > pluck('value','label') - > toArray();
i'm using Laravel 5.3
and i'm trying to select from the database the values for the lang files,
i created a file and named it global.php
and inside the file i tried to do this:
use App\Dictionary;
$language_id = 1;
$dictionary = array();
$lables = Dictionary::where('language_id',$language_id)->get();
foreach($lables as $label):
$dictionary[$label->label] = $label->value;
endforeach;
return $dictionary;
now, this is working but i want to select the rows using the short_name field and not the id of the language
i want it to be something like this:
$lables = Dictionary::all()->language()->where('short_name', 'en')->get();
my database looks like this:
Languages
id
name // for example: English
short_name // for exmaple: en
Dictionary
id
key
value
language_id
and my models looks like this:
Language Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Language extends Model
{
use SoftDeletes;
protected $softDelete = true;
protected $dates = ['deleted_at'];
public function dictionary()
{
return $this->hasMany('App\Dictionary');
}
}
Dictionary Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Dictionary extends Model
{
protected $table = 'dictionary';
public function language()
{
return $this->belongsTo('App\Language');
}
}
thank you for your help!
!!!UPDATE!!!
i added 1 more table called
Labels
id
label_name
and change the dictionary table to:
Dictionary
id
lable_id
value
language_id
how can i make this work so i can pull the label_name instead of the label_id
$lables = Dictionary::whereHas('language', function($query) {
$short_name = basename(__DIR__);
$query->where('short_name', $short_name);
})->pluck('value', 'label_id')->toArray();
解决方案 You can use Laravel's whereHas()
function as:
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->get();
Update
If you want to get rid of your foreach()
then you can use Laravel's pluck()
function as:
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->pluck('value', 'label')->toArray();
这篇关于Laravel 5.3雄辩的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!