Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel/Lumen关系-在条件检索错误的情况下连接其他表_Php_Mysql_Laravel_Lumen_Laravel Eloquent - Fatal编程技术网

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');