Laravel 8如何使用Hasmany在枢轴表上选择NOT NULL

发布于 2025-02-02 15:43:18 字数 807 浏览 4 评论 0原文

我会简单地我想选择所有具有 data 不是null的的所有用户

> 具有用户数据,但其他 ID 喜欢 ID 2、3、4 没有用户数据null[]或空数组

“在此处输入图像说明”

但我不知道要实现该目标的查询而且我不知道该代码控制器或模型?

,这是我的控制器

  public function index()
    {
        return  Project::with('users')->get();
    }

这是我的模型项目

   public function users(){
     return $this->belongsToMany(User::class)
     ->withPivot('salary')->
     ->using(project_user::class);
   }

希望您能可以帮助我

i will make it simple i want to select all the users where that have a data not null

like this in photo the ID 1 has a users data but the others ID like ID 2, 3 ,4 don't have a users data it's NULL or [] or empty Array

enter image description here

but i don't have a idea what kind of query to achieve that goal and i don't have a idea where to put that code CONTROLLER or MODEL?

so here is my CONTROLLER

  public function index()
    {
        return  Project::with('users')->get();
    }

here is my MODEL Project

   public function users(){
     return $this->belongsToMany(User::class)
     ->withPivot('salary')->
     ->using(project_user::class);
   }

hopefully you can help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2025-02-09 15:43:18

像这样?

 Project::whereHas('users')->with('users')->get();

Like this?

 Project::whereHas('users')->with('users')->get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文