代码之家  ›  专栏  ›  技术社区  ›  jose

withCount where条件中的eager loading pass变量

  •  -1
  • jose  · 技术社区  · 2 年前
    $user = 1;
                           
    Forum::with(['posts' => function($query){
        $query->withCount(['comments => function($query){
                $query->where('id_user', $user); 
            }]);
    }])
    ->get();
    

    如何将$user变量的值传递给 $query->where('id_user', $user);

    $user未工作(在编辑器中以红色下划线显示)。

    1 回复  |  直到 2 年前
        1
  •  1
  •   JEJ    2 年前
    $user = 1;
                           
    Forum::with(['posts' => function($query) use ($user){
        $query->withCount(['comments => function($query) use ($user){
                $query->where('id_user', $user); 
            }]);
    }])
    ->get();