SQLSTATE[42S22]:在 Laravel 7 高级过滤器中使用 when 子句时出错 sql

SQLSTATE[42S22]: Error using when clause in Laravel 7 advance filter with raw sql

已更新 我正在制作一个搜索过滤器并在查询生成器中使用原始 sql,因此数据库中的汽车过滤器有最小和最大里程和价格,假设列...... 实际上,我正在计算标准单位的价格和里程,即欧元和原始 sql 公里,以便在输出中应用运算符... 例如,如果用户存储 110 美元,那么它将首先在原始 sql 中转换为 100 欧元,然后我们将确定结果大于或小于... 该方法适用于一种情况,但是当我激活其他情况时,它会给出错误,指出找不到假定的列...

//query for min price:
    $product1 = DB::table('sellers') 
->leftJoin('users','sellers.user_id','=','users.id')
->leftJoin('prices_privates','sellers.id','=','prices_privates.user_id')
->when($request->get('price_min'), function($query) use ($request) {
                        $price = $request->get('price_min');
                        $usd = $this->currency(1 , 'USD');
                        $gbp = $this->currency(1 , 'GBP');
                        $cad = $this->currency(1 , 'CAD');
                        $aud = $this->currency(1 , 'AUD');
                        $mxn = $this->currency(1 , 'MXN');
                        $brl = $this->currency(1 , 'BRL');
                        $query->select("*",
                        \DB::raw('(CASE 
                       WHEN currency = "USD" THEN FORMAT((price * '.$usd.'),2)
                       WHEN currency = "GBP" THEN FORMAT((price * '.$gbp.'),2)
                       WHEN currency = "CAD" THEN FORMAT((price * '.$cad.'),2)
                       WHEN currency = "AUD" THEN FORMAT((price * '.$aud.'),2)
                       WHEN currency = "MXN" THEN FORMAT((price * '.$mxn.'),2)
                       WHEN currency = "BRL" THEN FORMAT((price * '.$brl.'),2)
                       WHEN currency = "EUR" THEN FORMAT((price * 1),2)
                       END) AS price_conv'))
                  ->having("price_conv" ,'>=', $price);
                })   
    //query for min mileage
 ->when($request->get('mileage_from'), function($query) use ($request) {
                        $mileage_from = $request->mileage_from;
                        $query->select("*",
                        \DB::raw('(CASE 
                            WHEN mileage_in = "miles" THEN mileage * 1.60934 
                            WHEN mileage_in = "km" THEN mileage + 0 
                            END) AS kilometers'))
                       ->having("kilometers" ,'>=', $mileage_from);
                    })

 //Radius Search
                
                ->when($request->input('distance_km')&& $request->input('loc_latitude') && $request->input('loc_longitude') , function ($query) use ($request){

                    $lat = $request->get('loc_latitude');
                    $long = $request->get('loc_longitude');
                    $radius = $request->get('distance_km');
                    $query->select("*",
                         \DB::raw('( 6371 * acos( cos( radians(?) ) *
                          cos( radians( location_privates.latitude ) )
                          * cos( radians( location_privates.longitude ) - radians(?)
                          ) + sin( radians(?) ) *
                          sin( radians( location_privates.latitude ) ) )
                         ) AS distance', [$lat, $long, $lat]))
                        ->having("distance", '<=', $radius);
                        // ->orderBy("distance",'asc');

                })

    ->get(['sellers.*',
                    'users.user_type',
                    'prices_privates.price',
                    'prices_privates.currency',
                    'location_privates.address',
                    'location_privates.latitude',
                    'location_privates.longitude'
                 ]);

如果我评论一个功能,另一个功能就可以了.. 他们不同时工作.. 此问题仅适用于包含原始 sql.

的查询

这里是整个函数:

    $product1 = DB::table('sellers') 
                        ->leftJoin('users','sellers.user_id','=','users.id')
                        ->leftJoin('location_privates','sellers.id','=','location_privates.user_id')
                        ->leftJoin('prices_privates','sellers.id','=','prices_privates.user_id')
                 
                                        
                    ->when($request->get('condition') == 'classic', function($query) use ($request) {
                        $year = Carbon::now()->format('Y');
                        $used = ($year - '20' );
                        $query->where('manufact_year', '<=', $used);
                    })  
                    ->when($request->get('condition') == 'used', function($query) use ($request) {
                        $year = Carbon::now()->format('Y');
                        $used = ($year - '20' );
                        $query->where('manufact_year', '>=', $used);
                    }) 
                    ->when($request->get('condition') == 'user', function($query) use ($request) {
                        $query->where('user_type', '=', 'user');
                    })
                    ->when($request->get('condition') == 'company', function($query) use ($request) {
                        $query->where('user_type', '=', 'company');
                    })

                    ->when($request->get('search_make'), function($query) use ($request) {
                        $make = $request->get('search_make');
                        $query->where('make', 'like', "%$make%");
                    })
                    
                    ->when($request->get('style'), function($query) use ($request) {
                        $style = $request->get('style');
                        $query->where('car_body', 'like', "%$style%");
                    })
                    ->when($request->get('model'), function($query) use ($request) {
                        $model = $request->get('model');
                        $query->where('model', 'like', "%$model%");
                    })
    
                    
                    ->when($request->get('color'), function($query) use ($request) {
                        $color = $request->get('color'); 
                        $query->where('color', 'like', "%$color%");
                    })
                    ->when($request->get('country'), function($query) use ($request) {
                        $country = $request->get('country');
                        $query->where('address', 'like', "%$country%");
                    })
                    
                    ->when($request->get('year_min'), function($query) use ($request) {
                        $query->where('manufact_year', '>=', $request->get('year_min'));
                    })
                    
                    
                    ->when($request->get('year_max'), function($query) use ($request) {
                        $query->where('manufact_year', '<=', $request->get('year_max'));
                    })
    
                  
                ->when($request->get('price_min') && $request->get('price_max'), function($query) use ($request) {
                    $price = $request->get('price_min');
                    $price_2 = $request->get('price_max');
                    $usd = $this->currency(1 , 'USD');
                    $gbp = $this->currency(1 , 'GBP');
                    $cad = $this->currency(1 , 'CAD');
                    $aud = $this->currency(1 , 'AUD');
                    $mxn = $this->currency(1 , 'MXN');
                    $brl = $this->currency(1 , 'BRL');

                    $query->select("*",

                    \DB::raw('(CASE 
                            

                        WHEN currency = "USD" THEN FORMAT((price * '.$usd.'),2)
                        WHEN currency = "GBP" THEN FORMAT((price * '.$gbp.'),2)
                        WHEN currency = "CAD" THEN FORMAT((price * '.$cad.'),2)
                        WHEN currency = "AUD" THEN FORMAT((price * '.$aud.'),2)
                        WHEN currency = "MXN" THEN FORMAT((price * '.$mxn.'),2)
                        WHEN currency = "BRL" THEN FORMAT((price * '.$brl.'),2)
                        WHEN currency = "EUR" THEN FORMAT((price * 1),2)
                        END) AS price_conv')
                        )
                        ->having('price_conv between $price and $price_2');
                        // ->having("price_conv" ,'<=', $price_2);
                     

                })
                
                // ->when($request->get('price_max'), function($query) use ($request) {
                //     $price_2 = $request->get('price_max');
                //     $usd = $this->currency(1 , 'USD');
                //     $gbp = $this->currency(1 , 'GBP');
                //     $cad = $this->currency(1 , 'CAD');
                //     $aud = $this->currency(1 , 'AUD');
                //     $mxn = $this->currency(1 , 'MXN');
                //     $brl = $this->currency(1 , 'BRL');

                //     $query->select("*",

                //     \DB::raw('(CASE 
                            

                //         WHEN currency = "USD" THEN FORMAT((price * '.$usd.'),2)
                //         WHEN currency = "GBP" THEN FORMAT((price * '.$gbp.'),2)
                //         WHEN currency = "CAD" THEN FORMAT((price * '.$cad.'),2)
                //         WHEN currency = "AUD" THEN FORMAT((price * '.$aud.'),2)
                //         WHEN currency = "MXN" THEN FORMAT((price * '.$mxn.'),2)
                //         WHEN currency = "BRL" THEN FORMAT((price * '.$brl.'),2)
                //         WHEN currency = "EUR" THEN FORMAT((price * 1),2)
                //         END) AS price_conv')
                //         )
                //         ->having('price_conv' ,'<=', $price_2);
                     

                // })

                        // WHEN currency = "miles" THEN 0

                // ->when($request->get('mileage_from'), function($query) use ($request) {
                //     $mileage_from = $request->mileage_from;

                //     $query->select("*",

                //     \DB::raw('(CASE 

                //         WHEN mileage_in = "miles" THEN mileage * 1.60934 

                //         WHEN mileage_in = "km" THEN mileage + 0 

                //         END) AS kilometers'))
                        
                //         ->having("kilometers" ,'>=', $mileage_from);
                // })
                
                // ->when($request->get('mileage_to') && $request->get('mileage_to') != '200000', function($query) use ($request) {
                //     $mileage_to = $request->get('mileage_to');
                //                     $query->select("*",

                //     \DB::raw('(CASE 

                //         WHEN mileage_in = "miles" THEN mileage * 1.60934 

                //         WHEN mileage_in = "km" THEN mileage + 0 

                //         END) AS kilometers'))

                //     ->having("kilometers", '<=', $mileage_to);
                // })
   
   //Radius Search
                
                // ->when($request->input('distance_km')&& $request->input('loc_latitude') && $request->input('loc_longitude') , function ($query) use ($request){

                //     $lat = $request->get('loc_latitude');
                //     $long = $request->get('loc_longitude');
                //     $radius = $request->get('distance_km');
                //     $query->select("*",
                //          \DB::raw('( 6371 * acos( cos( radians(?) ) *
                //           cos( radians( location_privates.latitude ) )
                //           * cos( radians( location_privates.longitude ) - radians(?)
                //           ) + sin( radians(?) ) *
                //           sin( radians( location_privates.latitude ) ) )
                //          ) AS distance', [$lat, $long, $lat]))
                //         ->having("distance", '<=', $radius);
                //         // ->orderBy("distance",'asc');

                // })

//End Radius Search

               
                ->get(['sellers.*',
                'users.user_type',
                'prices_privates.price',
                'prices_privates.currency',
                'location_privates.address',
                'location_privates.latitude',
                'location_privates.longitude'
                ]);

那部分:

 '*, ( 6371 * acos( cos( radians(?) ) * ' at line 11 (SQL: select *, (CASE WHEN currency = "USD" THEN FORMAT((price * 0.8149295086),2) WHEN currency = "GBP" THEN FORMAT((price * 1.112309934),2) WHEN currency = "CAD" THEN FORMAT((price * 0.6396724877),2) WHEN currency = "AUD" THEN FORMAT((price * 0.629089079),2) WHEN currency = "MXN" THEN FORMAT((price * 0.0409567497),2) WHEN currency = "BRL" THEN FORMAT((price * 0.1568996627),2) WHEN currency = "EUR" THEN FORMAT((price * 1),2) END) AS price_conv, *, ( 6371 * acos( cos( radians(13.7563309) ) * cos( radians( location_privates.latitude ) ) * cos( radians( location_privates.longitude ) - radians(100.5017651) ) + sin( radians(13.7563309) ) * sin( radians( location_privates.latitude ) ) ) ) AS distance from `sellers` left join `users` on `sellers`.`user_id` = `users`.`id` left join `location_privates` on `sellers`.`id` = `location_privates`.`user_id` left join `prices_privates` on `sellers`.`id` = `prices_privates`.`user_id` having `price_conv` >= 100 and `distance` >= 500)

您的 SQL 有两个 * 选择器,因此您应该删除多余的“星号”,只使用一个。

首先在查询的开头添加一个“all”选择器:

$product1 = DB::table('sellers')
    ->select("*")
//...

之后,将 \DB::raw 语句之前的所有 select("*", 替换为 addSelect(。像下面这样:

// Old code
$query->select("*",
        \DB::raw('( 6371 * acos( cos( radians(?) ) *
// New code
$query->addSelect(\DB::raw('( 6371 * acos( cos( radians(?) ) *

这将创建一个只有一个“全部”选择​​器的 SQL,并向其中添加每个自定义选择器。