i want to compare the search result in laravel controller.
When user input the name and search. In my controller it will remove the input spaces and compare data retrieved from database.
Users submitted is John Doe
preg_match_all('/\@([^\s\.]+)/', $request->name, $matches);
Remove spaces become JohnDoe. Then get data with
$user = User::where('name', $name)->first();
And i want remove the name column spaces before comparing the result. For example the data get from database column is John Doe. How to remove the spaces and comparing them.
$user = User::where('John Doe', $matches)->first();
Well I would say:
There is a problem if you are saving a different data to the one you really need. I mean you can easily save JohnDoe and its easy, and also makes life easy.
You can use Raw sql query with whereRaw()
method as such:
User::whereRaw("REPLACE(`name`, ' ', '') = ? ", $name)->first();
The REPLACE()
removes all whitespace and with no space. Find more about mysql REPLACE()
here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With