Skip to content

Commit

Permalink
Improved paginated query
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslopezj committed Oct 21, 2014
1 parent d4b3f02 commit 4ac6a2f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 58 deletions.
38 changes: 15 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand Down
34 changes: 0 additions & 34 deletions src/Nicolaslopezj/Searchable/DBHelper.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/Nicolaslopezj/Searchable/SearchableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function scopeSearch($query, $search) {

$this->makeJoins($query);

$this->makeGroupBy($query);

return $query;
}

Expand All @@ -54,6 +56,9 @@ protected function getColumns() {
* @return array
*/
protected function getJoins() {
if (!array_key_exists('joins', $this->searchable)) {
return [];
}
return $this->searchable['joins'];
}

Expand All @@ -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
Expand Down

0 comments on commit 4ac6a2f

Please sign in to comment.