Support the ongoing development of Laravel.io →
Database Eloquent

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

Last updated 3 years ago.
0

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

Last updated 10 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

visualight visualight Joined 30 Dec 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.