I have tables named buildingsandflats
building table
Building_Id | Building_Name | .......| Building_Owned_By | ....
apartment table
Flat_Id | Flat_Name | ........| Fk_Building_Id | .....
and in my models
Building
class Building extends Eloquent {
protected $primaryKey = "Building_Id";
protected $table = 'buildings';
.......
.......
public function flat()
{
return $this->hasMany('Flat', 'Fk_Building_Id', 'Building_Id');
}
}
Flat
class Flat extends Eloquent {
protected $primaryKey = "Flat_Id";
protected $table = 'flats';
.......
.......
public function building() {
return $this->belongsTo('Building','Fk_Building_Id', 'Building_Id');
}
}
and in my controller
$flats = Flat::where('Fk_Building_Id', '=',$buildingid)
->where('Building_Owned_By', '=',Auth::user()->Login_Id)
->orderBy('Flat_Name')
->get(array('Flat_Id as flatId', 'Flat_Name as flatName'))
->toArray();
But he does not return anything.
How can we do internal joins in Eloquent Orm (I don't want to use a free request)?
Update
Thanks for @Creator for his valuable time and help. He helps me find this. The solution is that we should use where Has , and we rewrite the code as
$flats = Flat::whereHas('building', function($q){
$q->where('Building_Owned_By', '=',Auth::user()->Login_Id);
})
->where('Fk_Building_Id', '=',$buildingid)
->orderBy('Flat_Name')
->get(array('Flat_Id as flatId', 'Flat_Name as flatName'))
->toArray();