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