Builder.php第2435行中的错误: BadMethodCallException: 调用未定义的方法Illuminate \ Database \ Query \ Builder :: exchanges()
有两个表,名为EXCHANGES表和FINALTRADES表。
交换表有START_TIME字段(格式为00:00:00), finaltrades表有exchange_id 。
我想只计算从最终交易表中start_time到start_time + 1hrs之间的条目。例如:如果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');
}
答案 0 :(得分:0)
以下应该可以解决问题:
$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();