Skip to content
Advertisement

laravel search multiple words separated by space

I am new to laravel query builder, I want to search multiple words entered in an input field for example if I type “jhon doe” I want to get any column that contains jhon or doe

I have seen/tried solutions using php MySQL but can’t able to adapt to query builder

JavaScript

how do I do this using query builder

this is what i have so far, what is the proper way of doing this,

JavaScript

please help, thanks in advance

Advertisement

Answer

This is how you do it with QueryBuilder, but first some additional notes:

JavaScript

That said, you obviously can’t rely on explode because in the above case you would get all the rows.

So, this is what you should do:

JavaScript

There is closure in the where because it is a good practice to wrap your or where clauses in parentheses. For example if your User model used SoftDeletingScope and you would not do what I suggested, your whole query would be messed up.

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