如何在 Laravel 的 Eloquent ORM 中编写内部连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21900679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:04:16  来源:igfitidea点击:

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

phplaravellaravel-4

提问by manuthalasseril

I have a tables named buildingsand flats

我有一个名为buildings和的表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)?

我们如何在 Eloquent Orm 中执行内部连接(我不想使用流畅的查询)?

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 whereHasand we rewrite the code as

感谢@Creator 的宝贵时间和帮助。他帮我找到了很多。解决方案是我们必须使用whereHas并将代码重写为

$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();

回答by Anil Sharma

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();

回答by Shahid Ahmad

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();