Laravel 4 - 通过用户名找到雄辩的关系

时间:2015-01-29 13:41:48

标签: laravel eloquent

$picture = User::find(Session::get('user_id'))
         ->pictures()->where('approved', '=', '1')->first();

如何根据用户名修改此代码而不是Session :: get(' user_id')

感谢。

1 个答案:

答案 0 :(得分:1)

如果用户已登录,请尝试

$pictures = Auth::user()->pictures()->where('approved', '1')->first();

<强>更新

关于你的评论:

try {
    $pictures = User::where('name', $searchname)->firstOrFail()->pictures()->where('approved', '1')->first();
}
catch (Exception $e) {
    // stuff to do, if no user is found
}