Skip to content
Advertisement

how to use Laravel 8 query builder like eloquent for searching

I’m developing a simple CRM with Laravel 8 that needs to search in query string.

My query string would be look like:

JavaScript

Now the problem is how can I search in controller?

I want to do like this:

JavaScript

I wonder why its not working and what is the standard way to do this.

I’ve tried:

JavaScript

but that didn’t work either.

I’ve also tried:

JavaScript

This codes works perfectly but we may not be sending the “q” or “gender” URL params.

PS: Sorry for my bad English 🙂

Advertisement

Answer

If you want to conditionally add where statements to your query you can use the if statements like you are attempting to do or use the when method of the Eloquent/Query Builder:

JavaScript

Laravel 8.x Docs – Queries – Conditional Clauses when

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