Php Laravel/Lumen关系-在条件检索错误的情况下连接其他表
在比较两个日期时,我从数据库中检索一些数据时遇到了一个奇怪的问题 数据库结构: 用户、预订、预订\服务、服务\价格\历史记录 根据上表: 用户有许多预订,预订有许多服务,服务有许多价格历史记录 我需要接受所有用户预订,然后根据预订日期查找服务价格 问题是,当我添加日期比较条件时,它没有考虑它 守则:Php Laravel/Lumen关系-在条件检索错误的情况下连接其他表,php,mysql,laravel,lumen,laravel-eloquent,Php,Mysql,Laravel,Lumen,Laravel Eloquent,在比较两个日期时,我从数据库中检索一些数据时遇到了一个奇怪的问题 数据库结构: 用户、预订、预订\服务、服务\价格\历史记录 根据上表: 用户有许多预订,预订有许多服务,服务有许多价格历史记录 我需要接受所有用户预订,然后根据预订日期查找服务价格 问题是,当我添加日期比较条件时,它没有考虑它 守则: $users = User::with(['reservations' => function($query) use ($department_id, $date_start, $date_
$users = User::with(['reservations' => function($query) use ($department_id, $date_start, $date_end) {
// reservation services
$query->with(['reservation_services' => function($query) {
$query->join('reservations', 'reservation_services.reservation_id', 'reservations.id');
$query->join('services', 'reservation_services.service_id', 'services.id');
// Find the service price based on reservation date
$query->join('services_price_history', function($query) {
$query->on('reservations.salon_id', 'services_price_history.salon_id');
$query->on('reservation_services.service_id', 'services_price_history.service_id');
// this shoud be the condition that gives us the correct
// price based on the reservation date
$query->where('services_price_history.date_start', '<', 'reservations.date');
// then we use order and limit 1
});
$query->select(
'reservation_services.reservation_id',
'reservation_services.service_id',
'reservation_services.quantity',
'services_price_history.price',
'services_price_history.date_start',
'services.name',
DB::raw('(services_price_history.price * reservation_services.quantity) as total')
);
$query->orderBy('services_price_history.id', 'desc');
}]);
}])->get();
我真的不知道为什么,但这行代码
$query->where('services\u price\u history.date\u start',”而不是:
$query->where('services_price_history.date_start', '<', 'reservations.date');
$query->where('services\u price\u history.date\u start','It works of a charm!请你解释一下原因好吗?我看不出有什么区别,我有点沮丧。非常感谢!当使用where
时,这种情况下的第三个参数是value,它是使用PDO准备的语句自动绑定的,当使用whereRaw
时,没有pr代码中的epared语句显示出来了,所以现在它完全按照它的编写方式使用,这是您非常需要的谢谢。这是有意义的。任何想法我可以在$query->whereRaw('services\u price\u history.date\u start
?使用$query->limit(1)
或first()
这似乎行不通。
$query->where('services_price_history.date_start', '<', 'reservations.date');
$query->whereRaw('services_price_history.date_start < reservations.date');