-1
$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).

jose
  • 1,490
  • 3
  • 30
  • 65
  • 1
    Possible duplicate: https://stackoverflow.com/questions/14482102/passing-data-to-a-closure-in-laravel-4 – aceraven777 May 04 '22 at 12:18
  • Does this answer your question? [How to pass argument in callback function in php?](https://stackoverflow.com/questions/35035355/how-to-pass-argument-in-callback-function-in-php) – ManojKiran A May 04 '22 at 16:11

1 Answers1

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