Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove spaces before getting the result in laravel elequent

Tags:

php

laravel

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();

like image 472
masterhunter Avatar asked Sep 02 '25 10:09

masterhunter


1 Answers

Well I would say:

  1. 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.

  2. 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

like image 169
Oluwatobi Samuel Omisakin Avatar answered Sep 04 '25 03:09

Oluwatobi Samuel Omisakin