I want to join this 3 tables.
delegations
delegates
meals
$meals = Meal::join('delegates', 'delegates.delegates_id', '=', 'meals.delegates_id')
->join('delegates', 'delegates.delegations_id', '=', 'delegates.delegations_id')
->get();
write following code in controller
use DB;
write following query inside of controller function
$meals=DB::select("SELECT delegations .*, delegates .*, meals.*
FROM delegations
JOIN delegates
ON delegates.delegations_id = delegations.delegations_id
JOIN meals
ON meals.delegates_id = delegates.delegates_id");
You can do it by two ways:
1.- Raw querys
use DB;
public static function getRaw_data(){
return DB::select("SELECT d1.*, d2.*, m.*
FROM delegations d1, delegates d2, meals m
WHERE d1.delegations_id = d2.delegations_id AND d2.delegates_id = m.delegates_id );
}
2.-
use DB;
public static function getRaw_data(){
return DB::table('delegations')
->join('delegates','delegations.delegations_id','=','delegates.delegations_id')
->join('meals','delegates.delegates_id','=','meals.delegates_id')
->select('delegations.*','delegates.*','meals.*')
->get();
}
Why would you want to do it "manually" if your using laravel? check on this reference eloquent-relationship
in that case just define in models (these are concept examples, I cant be sure about your project structure):
Meal
function delegate(){
$this->belongsTo(Delegate::class);
}
Delegate
function delegation(){
$this->belongsTo(Delegate::class);
}
function meal(){
$this->hasOne(Meal::class);
}
Delegation
function delegates(){
$this->hasMany(Delegate::class);
}
and then you could just do:
$meals = Meal::all();
foreach ($meals as $meal){
$delegates = $meal->delegate;
$delegation = $mealt->delegate->delegation;
}
you can use it on the controller o on blades and then you'll have all the info you need for instance $delegate->name, $delegation->id an so on