Home > Software design >  eager loading pass variable in withCount where condition
eager loading pass variable in withCount where condition

Time:05-05

$user = 1;
                       
Forum::with(['posts' => function($query){
    $query->withCount(['comments => function($query){
            $query->where('id_user', $user); 
        }]);
}])
->get();

How to pass the value of the $user variable to $query->where('id_user', $user);

$user is not working (appears underlined in red in the editor).

CodePudding user response:

$user = 1;
                       
Forum::with(['posts' => function($query) use ($user){
    $query->withCount(['comments => function($query) use ($user){
            $query->where('id_user', $user); 
        }]);
}])
->get();
  • Related