Hello, i try to get all datas (relation inlcude) from a user table but i cannot get somme values.
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'users';
protected $hidden = array('password', 'remember_token');
protected $fillable = array('matricule', 'ip_address', 'firstname', 'lastname', 'username', 'email', 'adress', 'tel', 'gsm', 'password', 'sex', 'code', 'active');
public function horaire(){
return $this->belongsToMany('Classe', 'horaires', 'user_id', 'classe_id')
->withPivot('jour', 'start');
}
}
If i use : $users = User::with('horaire')->get(); in my controller, i can get all data from USER table but not data from "horaire" table.
Here is the structure of my table : horaires [id] [user_id] [classe_id] [cour_id] [description]
Here is the structure of my table : classes [id] [code] [description]
What i want to do : fetch all user with all data from classes and horaires and understand how to call correctly the data from the relation. (example: $users->firstname work fine but $users->relations->horaire->classe->code doesn't work)
Thanks for your help
Hello, the only way in found is to make like this :
$users = User::select('users.firstname', 'users.lastname', 'horaires.jour', 'horaires.start', 'classes.code as code_classe', 'classes.classe', 'cours.code as code_cour', 'cours.cour')
->rightJoin('horaires', function($join) {
$join->on('horaires.user_id', '=', 'users.id');
})
->leftJoin('classes', function($join2) {
$join2->on('horaires.classe_id', '=', 'classes.id');
})
->leftJoin('cours', function($join3) {
$join3->on('horaires.cour_id', '=', 'cours.id');
})
->get();
What is the equivalent with belongsto and other eloquent method ?
Thanks
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community