2
我是laravel的新手,我想知道如何通過當前lang_code(語言表的一列)獲取language_id(數據庫中的主鍵「id」)。laravel 5在控制器中獲取當前語言ID
在我的語言模型,我創造了這樣的功能:在HomeController的
public function getLang($lang_code = null) {
$lang_code = App::getLocale();
return SELF::with($lang_code)->get();
}
,我加入這行代碼:
$lang = Language::with('getLang');
echo $lang->id;
有錯誤報道:
at HandleExceptions->handleError('8', 'Undefined property: Illuminate\Database\Eloquent\Builder::$id', '/home/otherraylinux/public_html/starter/app/Http/Controllers/HomeController.php', '52', array('articles' => object(Collection), 'lang' => object(Builder))) in HomeController.php line 52
編輯 我想要做的事情是:
$lang = DB::table('languages')->select('id')->where('lang_code', '=', App::getLocale())->get();
echo $lang[0]->id;
編輯
問題是什麼?獲得ID的最佳做法是什麼?我知道上面的代碼在實踐中不應該很好。
你能告訴我你的語言表的遷移文件嗎?,並嘗試返回不回顯 – Arlind 2015-04-05 10:29:01
@Arlind here https://github.com/mrakodol/Laravel-5-Bootstrap-3-Starter-Site/blob/主/數據庫/遷移/ 2014_10_18_195027_create_languages_table.php – hkguile 2015-04-05 10:31:32