[英]Using variable in where clause
现在我在app / routes.php中定义了这个:
View::share('user_image_count', Items::where('user_id', '=', 1)->count());
它返回数字9
,正是我想要的。
我想使其动态化,使其基于已登录/已认证的用户的user_id。
我目前在自己的观点中使用它:
{{ Auth::user()->user_id }}
如何在route.php中调整查询,使其使用经过身份验证的用户的ID号?
您是否尝试过以下方法?
if (Auth::check())
{
$user_id = Auth::user()->user_id;
$items_count = Items::where('user_id', '=', $user_id)->count();
View::share('user_image_count', $items_count);
}
我相信它应该起作用。
您可以尝试如下操作:
View::share('user_image_count', function(){
return Auth::check() ?
Items::where('user_id', '=', Auth::user()->user_id)->count() : '';
});
您认为:
{{ $user_image_count() }}
我认为您可以通过辅助课程更好地做到这一点。
比方说帮手
class Helper{
public static function GetUserCount($user_id){
return View::share('user_image_count', Items::where('user_id',$user_id)->count());
}
}
在您的视图中调用该类并将user_id传递给它
Echo this in view {{Helpers::GetUserCount(Auth::user()->user_id)}}
因此,这种方式对于所有登录用户而言都是动态的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.