簡體   English   中英

在where子句中使用變量

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM