Skip to content
Advertisement

How to have string query and date query at the same time in Laravel with Eloquent

At first my code works fine with just one parameter when querying, but when I add more parameters, it start to throw errors.

Here is MyEditController file:

JavaScript

Here is my web.php :

JavaScript

Here is my Blade:

JavaScript

Here is My Controller:

JavaScript

When I adding Some Parameter using this code it start showing error:

JavaScript

When I filter it with date it work fine but when I start to querying with strings it showing this error below:

enter image description here

How Can i fix that please ?

Thank You….

Advertisement

Answer

The parameter needs to be named:

JavaScript

Also make sure you’re getting matches. $datas->id is probably null. When paginating you can check $data->total() if it’s 0 that means there’s no matches when you’re filtering by a keyword.

Edit: So it looks like your when logic overrides the select logic:

JavaScript

In the above, when you before were getting *, you now no longer get the id field. You should add the fields which you need:

JavaScript

Edit2:

Regarding the outside date range, there’s no parenthesis within the query that will get generated. You’d want to wrap the searching logic within parenthesis otherwise the OR’s would check whichever is true, and short circuit before the date range check.

Instead of this:

JavaScript

Wrap the search within a where like so:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement