Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
relationships with multiple pivot table Eloquent/Laravel

I have 4 table

users
 id
 name

school
 id
 name

user_school
 id
 user_id
 school_id

user_role
 id
 user_role
 role_id

We need to find school users list whose have role id is 2

we use belongsToMany but not success

public function totalAdmin(){
        return $this->belongsToMany('App\Models\User','user_school', 'school_id','user_id')->where('user_school.status',1)->where('user_school.deleted',0);
    }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Assuming you have relationships:

  • User-Role named roles (with pivot user_role)
  • School-User named users (with pivot user_school)

You can do:

School::users()->whereHas('roles', function ($q) {
   $q->where('id', 2);
});

You can probably transfer it in the relationship as well:

public function totalAdmin(){
    return $this->belongsToMany('App\Models\User','user_school','school_id','user_id')
      ->whereHas('roles', function($q) { $q->where('id', 2); });
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda