Home > Net >  laravel 8 how to select not null on pivot table using hasMany
laravel 8 how to select not null on pivot table using hasMany

Time:05-30

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

CodePudding user response:

Like this?

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