From 4ac6a2f20e900e19ac56815e500a8b4a59351cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20L=C3=B3pez?= Date: Tue, 21 Oct 2014 13:14:47 -0300 Subject: [PATCH] Improved paginated query --- README.md | 38 ++++++++----------- composer.json | 2 +- src/Nicolaslopezj/Searchable/DBHelper.php | 34 ----------------- .../Searchable/SearchableTrait.php | 14 +++++++ 4 files changed, 30 insertions(+), 58 deletions(-) delete mode 100644 src/Nicolaslopezj/Searchable/DBHelper.php diff --git a/README.md b/README.md index b35a683..170a77e 100755 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Searchable is a trait for Laravel 4.2+ that adds a simple search function to Elo Searchable allows you to perform searches in a table giving priorities to each field for the table and it's relations. -This is not optimized for big searches, but sometimes you just need to make it simple. +This is not optimized for big searches, but sometimes you just need to make it simple (Although it is not slow). # Installation @@ -68,35 +68,27 @@ $users = User::search($query) ## Search Paginated -Laravel default pagination doesn't work with this, you have to do it this way +As easy as laravel default queries ```php -// This class is required -use Nicolaslopezj\Searchable\DBHelper; +// Search with relations and paginate +$users = User::search($query) +->with('posts') +->paginate(20); ``` +## Mix queries + +Search method is compatible with any eloquent method. You can do things like this: + ```php -// Get the current page values -$page = Input::get('page') ? Input::get('page') : 1; -$count = Input::get('count') ? Input::get('count') : 20; // items per page -$from = 1 + $count * ($page - 1); - -// Perform the search -$data = User::search($query) -->take($count) -->skip($from - 1) -->get() -->toArray(); - -// Get the count of rows of the last query -$db_query_log = DB::getQueryLog(); -$db_query = end($db_query_log); -$total_items = DBHelper::getQueryCount($db_query); - -// Create the paginator -$users = Paginator::make($data, $total_items, $count); +// Search only the active users +$users = User::where('status', 'active') +->search($query) +->paginate(20); ``` + # How does it works? Searchable builds a query that search through your model using Laravel's Eloquent. diff --git a/composer.json b/composer.json index f626c53..511dd08 100755 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "nicolaslopezj/searchable", "description": "Eloquent model search trait.", - "keywords": ["laravel", "eloquent", "search", "searchable"], + "keywords": ["laravel", "eloquent", "search", "searchable", "database", "model"], "license": "MIT", "authors": [ { diff --git a/src/Nicolaslopezj/Searchable/DBHelper.php b/src/Nicolaslopezj/Searchable/DBHelper.php deleted file mode 100644 index 3eb1824..0000000 --- a/src/Nicolaslopezj/Searchable/DBHelper.php +++ /dev/null @@ -1,34 +0,0 @@ -count; - - return intval($count); - } -} \ No newline at end of file diff --git a/src/Nicolaslopezj/Searchable/SearchableTrait.php b/src/Nicolaslopezj/Searchable/SearchableTrait.php index 00e98ff..fd45b8e 100755 --- a/src/Nicolaslopezj/Searchable/SearchableTrait.php +++ b/src/Nicolaslopezj/Searchable/SearchableTrait.php @@ -38,6 +38,8 @@ public function scopeSearch($query, $search) { $this->makeJoins($query); + $this->makeGroupBy($query); + return $query; } @@ -54,6 +56,9 @@ protected function getColumns() { * @return array */ protected function getJoins() { + if (!array_key_exists('joins', $this->searchable)) { + return []; + } return $this->searchable['joins']; } @@ -67,6 +72,15 @@ protected function makeJoins(&$query) { } } + /** + * Make the query dont repeat the results + * @param $query + */ + protected function makeGroupBy(&$query) { + $primary_key = $this->primaryKey; + $query->groupBy($primary_key); + } + /** * Puts all the select clauses to the main query * @param $query