Searching for Bobby Fisher with Laravel 5
TNT Studio has a good tutorial about how to use TNTSearch package to search full-text of documents quickly.
In modern web applications, a common requirement is a search feature. Clients are spoiled by Google and other search engines and expect a powerfull search experience in their own products. In this tutorial, we'll cover how to search your user base and sequentially make yourself or your clients happy.
If you worked on a medium sized or larger application, assuming you have a typical "users" table, you came up with a solution yourself like:
public function scopeSearchByKeyword($query, $keyword) { if ($keyword!='') { $query->where(function ($query) use ($keyword) { $query->where("firstname", "LIKE","%$keyword%") ->orWhere("lastname", "LIKE", "%$keyword%") ->orWhere("email", "LIKE", "%$keyword%") ->orWhere("phone", "LIKE", "%$keyword%"); }); } return $query; }
This will certainly work for one keyword, but you'll face problems when you try to search by multiple keywords like Bobby Fischer. So you digg deeper into your toolbox and write something like:
$users = User::where(function ($q) use ($query) { $q->where(DB::raw('CONCAT( firstname, " ", lastname)'), 'like', '%' . $query . '%') ->orWhere(DB::raw('CONCAT( lastname, " ", firstname)'), 'like', '%' . $query . '%') ->orWhere('emai
Link: