sto usando Laravel 5.3
e sto provando a select dal database i valori per i file lang,
Ho creato un file e l'ho chiamato global.php
e all'interno del file ho provato a fare questo:
use App\Dictionary; $language_id = 1; $dictionary = arrays(); $lables = Dictionary::where('language_id',$language_id)->get(); foreach($lables as $label): $dictionary[$label->label] = $label->value; endforeach; return $dictionary;
ora funziona, ma voglio select le righe usando il field short_name e non l'id della lingua
voglio che sia qualcosa del genere:
$lables = Dictionary::all()->language()->where('short_name', 'en')->get();
il mio database assomiglia a questo:
Le lingue
id name // for example: English short_name // for exmaple: en
Dizionario
id key value language_id
e i miei templates assomiglia a questo:
Modello di lingua
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'); } }
Modello del dictionary
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Dictionary extends Model { protected $table = 'dictionary'; public function language() { return $this->belongsTo('App\Language'); } }
grazie per l'aiuto!
!!! AGGIORNAMENTO !!!
Ho aggiunto 1 altra tabella chiamata
etichette
id label_name
e cambia la tabella del dictionary in:
Dizionario
id lable_id value language_id
come posso fare questo lavoro così posso tirare il label_name invece di label_id
$lables = Dictionary::whereHas('language', function($query) { $short_name = basename(__DIR__); $query->where('short_name', $short_name); })->pluck('value', 'label_id')->toArray();
Puoi usare la function whereHas()
di whereHas()
come:
$lables = Dictionary::whereHas('language', function($query) { $query->where('short_name', 'en'); })->get();
Aggiornare
Se vuoi sbarazzarti del tuo foreach()
allora puoi usare la function di pluck pluck()
di Laravel come:
$lables = Dictionary::whereHas('language', function($query) { $query->where('short_name', 'en'); })->pluck('value', 'label')->toArray();
Immagino che potrebbe essere qualcosa di simile:
$lables = Dictionary::with('language')->where('short_name', 'en')->get();