调用未定义的方法 Illuminate\Database\Query\Builder::exchanges()

Call to undefined method Illuminate\Database\Query\Builder::exchanges()

ERROR: BadMethodCallException in Builder.php line 2435: Call to undefined method Illuminate\Database\Query\Builder::exchanges()

有两个 table,分别命名为 EXCHANGES table 和 FINALTRADES table.

交易所 table 有 START_TIME 字段(格式:00:00:00)和 finaltrades table 有 exchange_id.

我只想计算距离最终交易 table start_time 到 start_time+1 小时之间的条目。例如:如果 start_time 有 09:15:00,则只计算 09:15:00 到 10:15:00 条记录。

函数:

public function getCountHour() {
       $user_id = Auth::user()->id;
       $countTrades = FinalTrade::where('user_id', '=', $user_id)
            ->exchanges()
            ->where('start_time', '>=', $your_desired_time)
            ->where('start_time' , '<=' , strtotime($your_desired_time) + 60*60)
            ->count();

                return response()->json($countTrades);

    }

与 FnalTrade 模型的关系:

   public function exchanges(){
   return $this->hasOne('App\EXCHANGE_MODEL_NAME', 'id', 'exchange_id');
}

both table SCREEN

以下应该可以解决问题:

$countTrades = FinalTrade::whereUserId(Auth::id())
  ->whereHas('exchanges', function($query) use ($your_desired_time) {
    $query
      ->where('start_time', '>=', $your_desired_time)
      ->where('start_time' , '<=' , strtotime($your_desired_time) + 60*60);
})
->count();