Roy 2019 Roy 2019 - 5 months ago 124
PHP Question

laravel 5.2 - search function

I wish to make a custom search on my site.

Here is MySQL Search:


SELECT * FROM
MyDB
.
MyTable
WHERE (
id
LIKE '%MySearch%' OR
firstname
LIKE '%MySearch%' OR
lastname
LIKE '%MySearch%' OR
email
LIKE '%MySearch%' OR
address
LIKE '%MySearch%');


How can I get that search into my Laravel Controller??

$users = DB::table('MyTable')->where('firstname', 'MySearch')->get();

My Laravel Controller

Thanks in advance !

Answer

for laravel 5.2:

$search = '%'.$_POST['name'].'%';

$users = DB::table('MyTable')
         ->where('id', 'LIKE', $search)
         ->orwhere('firstname', 'LIKE', $search)
         ->orwhere('lastname', 'LIKE', $search)
         ->orwhere('email', 'LIKE', $search)
         ->orwhere('address', 'LIKE', $search);
         ->get();

It work !

Comments