2017-07-28 62 views
0

我有以下與正常工作的子查詢工作查詢我想transfrom以雄辯的,因爲我想堅持我的用戶模型的其他relaiont將原始查詢與子查詢侃侃而談

select * from (
select p.from, a.account_id, count(*) as num 
from accounts as a 
inner join accounts_prop as p on (a.account_id = p.account_id) 
group by p.account_id 
having num = 1) query 

where year(query.von) = 2017 

我已經有由(表accounts)表示的用戶模型,並通過(properites表)

User -> Property關係所表示的屬性的模型是一對多

用戶

public function properties() 
{ 
    return $this->hasMany('App\Property', 'account_id', 'account_id'); 
} 

物業

public function user() 
    { 
     return $this->belongsTo('App\User', 'account_id'); 
    } 

輸出應該歸還它有一個屬性和屬性當前日期相匹配的所有用戶。

在我的測試目的原始查詢我所用恆定的一年

+0

工作,請發表您的表結構和發佈查詢您的預計產量。 –

回答

0

其實好像

User::whereHas('properties', function($query){ 

      $query = $query->where('von', '=', Carbon::now()->format('Y-m-d')) 
          ->havingRaw('count(account_id) = 1') 
          ->with('department'); 

     })->get();