Php Laravel 5.3雄辩的关系
我用的是Laravel 5.3 我试图从数据库中选择lang文件的值 我创建了一个文件,并将其命名为global.php 在文件中,我尝试这样做:Php Laravel 5.3雄辩的关系,php,laravel,eloquent,laravel-5.3,Php,Laravel,Eloquent,Laravel 5.3,我用的是Laravel 5.3 我试图从数据库中选择lang文件的值 我创建了一个文件,并将其命名为global.php 在文件中,我尝试这样做: use App\Dictionary; $language_id = 1; $dictionary = array(); $lables = Dictionary::where('language_id',$language_id)->get(); foreach($lables as $label): $dictionar
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;
现在,这是可行的,但是我想使用short_name字段而不是语言的id来选择行
我希望它是这样的:
$lables = Dictionary::all()->language()->where('short_name', 'en')->get();
我的数据库如下所示:
$lables = Dictionary::all()->language()->where('short_name', 'en')->get();
语言
id
name // for example: English
short_name // for exmaple: en
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');
}
}
id
label_name
字典
id
key
value
language_id
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');
}
}
id
lable_id
value
language_id
我的模型是这样的:
$lables = Dictionary::all()->language()->where('short_name', 'en')->get();
语言模型
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');
}
}
字典模型
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');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Dictionary extends Model
{
protected $table = 'dictionary';
public function language()
{
return $this->belongsTo('App\Language');
}
}
并将字典表更改为:
字典
id
key
value
language_id
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');
}
}
id
lable_id
value
language_id
我如何才能做到这一点,以便我可以提取标签名称而不是标签id
$lables = Dictionary::whereHas('language', function($query) {
$short_name = basename(__DIR__);
$query->where('short_name', $short_name);
})->pluck('value', 'label_id')->toArray();
我想可能是这样的:
$lables = Dictionary::with('language')->where('short_name', 'en')->get();
我想可能是这样的:
$lables = Dictionary::with('language')->where('short_name', 'en')->get();
您可以将Laravel的
whereHas()
函数用作:
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->get();
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->pluck('value', 'label')->toArray();
更新
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');
}
}
如果你想摆脱你的foreach()
,那么你可以使用Laravel的pluck()
函数作为:
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->get();
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->pluck('value', 'label')->toArray();
您可以将Laravel的
whereHas()
函数用作:
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->get();
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->pluck('value', 'label')->toArray();
更新
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');
}
}
如果你想摆脱你的foreach()
,那么你可以使用Laravel的pluck()
函数作为:
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->get();
$lables = Dictionary::whereHas('language', function($query) {
$query->where('short_name', 'en');
})->pluck('value', 'label')->toArray();
更新:我接受了您的代码并添加了这一行,因此我不需要在lang文件夹$lables=Dictionary::whereHas('language',function($query){$short_name=basename(DIR);$query->where('short_name',$short_name);})->pulk('value',label')->toArray();你能看看我的更新并帮我吗@AmitGupathis没有直截了当的答案,要么使用join,要么使用foreach。因此我必须使用循环并删除“toArray()”?是的,必须删除
pulk()
和toArray()
并使用get()
only.a更新:我接受了您的代码并添加了这一行,这样我就不需要在lang文件夹$lables=Dictionary::whereHas('language',function($query){$short_name=basename(DIR);$query->where('short_name',$short_name);})->pull('value',label')->toArray();你能看看我的更新并帮我吗@AmitGupathis没有直截了当的答案,要么使用join,要么使用foreach。因此,我必须使用循环并删除“toArray()”?是的,您必须删除pulk()
和toArray()
并仅使用get()
。