Laravel / 流明关系 - join other table where condition retrieve wrong
Laravel / Lumen relationship - join other table where condition retrieve wrong
我在比较两个日期时从数据库中检索一些数据时遇到了一个奇怪的问题。
数据库结构:
用户,预订,reservation_services,services_price_history
根据上表:
一个用户有很多预订,一个预订有很多服务,一个服务有很多历史价格。
我需要获取所有用户预约,然后根据预约日期查找服务价格
问题是当我添加日期比较条件时它没有考虑到它
代码:
$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();
这是输出:
[
{
"id":10,
"first_name":"Mihaela",
"last_name":"Radulescu",
"reservations":[
{
"id":112,
"salon_id":2,
"client_id":161,
"user_id":10,
"date":"2017-10-31", // the reservation date
"start":"10:00",
"end":"12:00",
"state_id":5,
"notes":null,
"price":"350.00",
"reservation_services":[
{
"reservation_id":112,
"service_id":89,
"quantity":1,
"price":"400.00",
"date_start":"2017-11-02", // this is wrong
"name":"Tratament ANTI AGE",
"total":"400.00"
},
{
"reservation_id":112,
"service_id":89,
"quantity":1,
"price":"350.00",
"date_start":"2017-10-19", // this is ok
"name":"Tratament ANTI AGE",
"total":"350.00"
}
],
"reservation_products":[
]
},
]
},
],
我真的不知道为什么,但是代码中的这一行
$query->where('services_price_history.date_start', '<', 'reservations.date');
没有正常工作,因为 '2017-11-02'
比 '2017-10-31'
高,但它显示在结果中。
非常感谢。
而不是:
$query->where('services_price_history.date_start', '<', 'reservations.date');
使用:
$query->whereRaw('services_price_history.date_start < reservations.date');
我在比较两个日期时从数据库中检索一些数据时遇到了一个奇怪的问题。
数据库结构: 用户,预订,reservation_services,services_price_history
根据上表: 一个用户有很多预订,一个预订有很多服务,一个服务有很多历史价格。
我需要获取所有用户预约,然后根据预约日期查找服务价格
问题是当我添加日期比较条件时它没有考虑到它
代码:
$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();
这是输出:
[
{
"id":10,
"first_name":"Mihaela",
"last_name":"Radulescu",
"reservations":[
{
"id":112,
"salon_id":2,
"client_id":161,
"user_id":10,
"date":"2017-10-31", // the reservation date
"start":"10:00",
"end":"12:00",
"state_id":5,
"notes":null,
"price":"350.00",
"reservation_services":[
{
"reservation_id":112,
"service_id":89,
"quantity":1,
"price":"400.00",
"date_start":"2017-11-02", // this is wrong
"name":"Tratament ANTI AGE",
"total":"400.00"
},
{
"reservation_id":112,
"service_id":89,
"quantity":1,
"price":"350.00",
"date_start":"2017-10-19", // this is ok
"name":"Tratament ANTI AGE",
"total":"350.00"
}
],
"reservation_products":[
]
},
]
},
],
我真的不知道为什么,但是代码中的这一行
$query->where('services_price_history.date_start', '<', 'reservations.date');
没有正常工作,因为 '2017-11-02'
比 '2017-10-31'
高,但它显示在结果中。
非常感谢。
而不是:
$query->where('services_price_history.date_start', '<', 'reservations.date');
使用:
$query->whereRaw('services_price_history.date_start < reservations.date');