How to build the query with selectRaw and whereRaw having php vars, table columns

hello,
I have a Laravel rather complex question: // Rent a car search
How to build the query with selectRaw and whereRaw having php vars, table column names, and php vars as a string that contain the name of column (integer in mysql)…?
I have read here:
https://laravel.com/docs/7.x/queries#raw-expressions
but getting runtime error selectRaw and syntax error for the two last lines – now commented… any hint???
In other words how contract query with calculation involved PHP vars PHP vars string(column name(integer)) or column name plain…?

	
        $rate="prices.h";
        $carsPrices = DB::table('cars')
        ->join('prices', 'cars.cat', '=', 'prices.cat')
        ->selectRaw('*, (( ? * ? )-( ? * ? ) * (cars.discount + 20) / 100 ) as price, ? as rate, ? as days', [$rate, $days, $rate, $days, $rate, $days])  //->get();  // correct
        ->where([
            ['cars.people', '>=', $peopleMin],
            ['cars.people', '<=', $peopleMax],
            ['cars.doors', '>=', $doorsMin],
            ['cars.doors', '<=', $doorsMax],
        ])->get();  // work till here --- but runtime error price = 0 selectRaw not work
        //->whereRaw('(( "$rate" * $days ) - ( "$rate"  * $days ) * cars.discount / 100 ) * 1.19' , '<=', $priceLow)->get();
        //->whereRaw( $priceHigh, '>=', '(( ? * ? ) - ( ?  * ? ) * cars.discount / 100 ) * 1.19', [$rate, $days, $rate, $days])->get();

Illuminate\Database\QueryException

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘0 (( “$rate” * $days ) - ( “$rate” * $days ) * cars.discount / 100 ) * 1.19’ at line 1 (SQL: select *, (( prices.h * 7 )-( prices.h * 7 ) * (cars.discount + 20) / 100 ) as price, prices.h as rate, 7 as days from cars inner join prices on cars.cat = prices.cat where (cars.people >= 2 and cars.people <= 4 and cars.doors >= 2 and cars.doors <= 5) 0 (( “$rate” * $days ) - ( “$rate” * $days ) * cars.discount / 100 ) * 1.19)

  1. As I know, whereRaw() signature has only two params: $sql and $args. That what you used to, looks like just where().

  2. If you use single qoutes, variables inserted in string not. It should be double quotes.

1 Like

WHAT ABOUT THIS…???

$days = integer
$rate = string name of MySQL column - integer in MySQL

price give 0…in all cars rental results — in var_dump() debugging? any hint? May the where clause effect it but Not sure…

I re-tried
if

        $rate="prices.h";  // price = 0 in var_dump
        $rate=9;  // price = <float number> in var_dump  // works - change in debugging
	
        ->selectRaw('*, (( ? * ? )-( ? * ? ) * (cars.discount + 20) / 100 ) as price

In other words is Not translated to column float value in the first case, well…?

I think works as — i am testing success - selectRaw the same

->whereRaw( '(( ' . $rate . ' * ? ) - ( ' . $rate . ' * ? ) * cars.discount / 100 ) * 1.19 <= ? ', [$days, $days, $priceHigh])->get();

this way [$days, $days, $priceHigh] numbers fixed
$rate name of db field mysql - mysql float

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.