3

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 !

Yooniks
  • 71
  • 1
  • 6
  • the answer at the bottom is correct( i was writing a full long answer but he was first, and i just want to mention why you use $_POST? i think its better to use the Request , its more laravel way (both are correct , just an advise) – Achraf Khouadja May 19 '16 at 03:05

4 Answers4

2
$users = DB::table('MyTable')
         ->where('id', 'LIKE', '%MySearch%')
         ->or_where('firstname', 'LIKE', '%MySearch%')
         ->or_where('lastname', 'LIKE', '%MySearch%')
         ->or_where('email', 'LIKE', '%MySearch%')
         ->or_where('address', 'LIKE', '%MySearch%');
         ->get();
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

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 !

Yooniks
  • 71
  • 1
  • 6
0

You can build queries with 'orwhere' like bellow

$users = DB::table('users')
                ->where('firstname', 'like', 'John%')
                ->orWhere('lastname', 'John')
                ->get();

Please refer to this document before building your queries. https://laravel.com/docs/5.2/queries

Praveen Govind
  • 2,619
  • 2
  • 32
  • 45
0

This one also works for me in laravel 5.2

    $search = '%'.$request->search.'%';
    $result = DB::table('tours')->where('name', 'like', $search)->get();