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)