I have a tables named buildings
and flats
buildings table
Building_Id | Building_Name | .......| Building_Owned_By | ....
flats 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 it returns nothing.
How can we perform inner joins in Eloquent Orm (I dont want to use fluent query)?
Update
Thanks for @Creator for his valuable time and help. He helps me a lot for finding this. The solution is we have to use whereHas 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();
laravel eloquent Join method by default use inner join. For example if you have 10 rows and you want the join from other table then it will only return the rows which satisfy both the tables. Laravel eloquent or query builder join method do the same as MySQL inner join function.
If you want to join two or multiple tables in laravel then you can use laravel eloquent join(), left join(), right join(), cross join(). And another option to join two or multiple table, you can use laravel eloquent relationships instead of laravel join.
Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.
Do this :
class Building extends Eloquent {
protected $primaryKey = "Building_Id";
protected $table = 'buildings';
.......
.......
public function flat()
{
return $this->HasMany('Flat', 'Fk_Building_Id', 'Building_Id');
}
}
Query to get building with all flats:
Building::with('flat')->(some_condition)->get();
class Flat extends Eloquent {
protected $primaryKey = "Flat_Id";
protected $table = 'flats';
.......
.......
public function building() {
return $this->HasOne('Building','Fk_Building_Id', 'Building_Id');
}
}
Query to get flat with building info
Flat::with('building')
->where('Building_Owned_By', '=',Auth::user()->Login_Id)
->orderBy('Flat_Name')
->get(array('Flat_Id as flatId', 'Flat_Name as flatName'))
->toArray();
Try this:
Flat::with(array('building'=>function($query){
$query->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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With