Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a inner join in Eloquent ORM in laravel?

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();
like image 707
manuthalasseril Avatar asked Feb 20 '14 07:02

manuthalasseril


People also ask

How use inner join in laravel eloquent?

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.

How do you join two tables with an eloquent relationship?

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.

What is eloquent ORM in laravel example?

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.


2 Answers

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();
like image 67
Anil Sharma Avatar answered Sep 20 '22 11:09

Anil Sharma


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();
like image 44
Shahid Ahmad Avatar answered Sep 19 '22 11:09

Shahid Ahmad