Reputation: 1131
Is it possible to use a string as it is such as:
$x = "field1 > 1 && field 2 LIKE '%z%'...";
Instead of breaking the above string into chunks like:
->where('field1', '>', 1)
->where('field2', 'LIKE', '%z%')
The reason is that I have many where
statements written like above in an old project and I would like to use them as they are in a new project with eloquent queries.
Upvotes: 0
Views: 431
Reputation: 5731
You can use whereRaw()
->where('field1 > 1 AND field2 LIKE "%z%"')
With variables
$str = 'z';
$field1 =1;
->where("field1 > $field1 AND field2 LIKE '%$str%' ")
Upvotes: 0
Reputation: 830
you can use eloquent with raw query like this :-
User::whereRaw('id = 1')->get()
or you can use DB raw query to do that like this :-
DB::select('select * from user where field > :field', ['field' => 1]);
you should avoid to concatenate vars come from request to your query so use bind to secure your query.
Upvotes: 5