我有一个名为buildingsflats的表

建筑表

Building_Id | Building_Name | .......| Building_Owned_By | ....

公寓表
Flat_Id | Flat_Name | ........| Fk_Building_Id | .....

和在我的模型中

建筑
class Building extends Eloquent {
    protected $primaryKey = "Building_Id";
    protected $table = 'buildings';
    .......
    .......
    public function flat()
    {
        return  $this->hasMany('Flat', 'Fk_Building_Id', 'Building_Id');
    }
}

扁平
class Flat extends Eloquent {
    protected $primaryKey = "Flat_Id";
    protected $table = 'flats';
    .......
    .......
    public function building() {
        return $this->belongsTo('Building','Fk_Building_Id', 'Building_Id');
    }
}

在我的 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();

但是它什么也没返回。

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

更新

感谢@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();

最佳答案

做这个 :

class Building extends Eloquent {
protected $primaryKey = "Building_Id";
protected $table = 'buildings';
.......
.......
public function flat()
{
    return  $this->HasMany('Flat', 'Fk_Building_Id', 'Building_Id');
}

}

查询以获取所有单位的建筑物:
   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');
   }
}

查询以获取建筑物信息
      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();

10-05 18:44