Binding Parameters in Laravel Raw DB Queries on Models
When working with raw DB queries in Laravel on models, binding parameters can be a challenge. This issue arises when using a combination of named and positional parameters, leading to the error "Invalid parameter number: mixed named and positional parameters."
To resolve this, consider the following solution:
$property = Property::select( DB::raw("title, lat, lng, ( 3959 * acos( cos( radians( ? ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians( ? ) ) * sin( radians( lat ) ) ) ) AS distance") ) ->having("distance", "<", "?") ->orderBy("distance") ->take(20) ->setBindings([$lat, $lng, $lat, $radius]) ->get();
By utilizing the setBindings() method, parameter values can be bound to the query, allowing for the execution of raw DB queries that require parameterization.
The above is the detailed content of How to Handle Binding Parameters in Laravel Raw DB Queries on Models?. For more information, please follow other related articles on the PHP Chinese website!