Skip to content
Advertisement

Using raw DB to query but fails to run because sql string has an error

Im new to using laravel and i have to query using the an sql string but the code fails to run because there is an error in the sql query The code is:

 $current_lat = $request->latitude;

  $current_lng = $request->longitude;

  $car_type = $request->car_type;
        $data=DB::table('artisantypes')->select(DB::raw('artisantypes.max_seat AS max_size,3956 * 2 * ASIN(SQRT(POWER(SIN(($current_lat - artisans.driver_lat) * pi()/180 / 2), 2)+ COS($current_lat * pi()/180 ) * COS(artisans.driver_lat * pi()/180) * POWER(SIN(($current_lng - artisans.driver_lng) * pi()/180 / 2), 2) )) as distance,artisans.driver_lat,artisans.driver_lng' ))
        ->leftjoin('artisans','artisantypes.id', '=','artisans.car_id' )
        ->where('artisantypes.id' ,'=', $car_type and 'artisans.status', '=' , 0)
        ->groupby('artisans.id')
        ->having('distance' ,'<', 25)
        ->orderby ('distance', 'ASC' )
        ->limit( 0,1)
        ->get();

The Error in got is :

 check the manual that corresponds to your MariaDB server version for the right syntax to use near '= `artisantypes`.`id` = ? group by `artisans`.`id` having `distance` < ? 

Advertisement

Answer

You cannot directly specify an AND in the where function.

change

->where('artisantypes.id' ,'=', $car_type and 'artisans.status', '=' , 0)

to

->where([
    ['artisantypes.id' ,'=', $car_type],
    ['artisans.status', '=' , 0],
])
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement