Skip to content

[feature] Add limit and offset methods #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage
.phpunit.result.cache
.idea
.env
*.log
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,25 @@ Search::add(Post::class, 'title')
->search('build');
```

### Offset and limit

You may get a range window by calling the `limit` method before the `search` method, and manipulate the range window with the `offset` method, and you'll get an instance of `\Illuminate\Database\Eloquent\Collection` as a result.

```php
Search::add(Post::class, 'title')
->add(Video::class, 'title')

// limit result data records
->limit($limit)

// skip result data records
->offset($offset)

->search('build');
```

When using the `limit` method or the `offset` method with the `paginate` method or the `simplePaginate` method throws an exception.

### Constraints and scoped queries

Instead of the class name, you can also pass an instance of the [Eloquent query builder](https://laravel.com/docs/master/eloquent#retrieving-models) to the `add` method. This allows you to add constraints to each model.
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/LimitAlreadyPassedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace ProtoneMedia\LaravelCrossEloquentSearch\Exceptions;

use Exception;

class LimitAlreadyPassedException extends Exception
{
public static function make(): static
{
return new static("You can't paginate if you set a limit.");
}
}
15 changes: 15 additions & 0 deletions src/Exceptions/OffsetAlreadyPassedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace ProtoneMedia\LaravelCrossEloquentSearch\Exceptions;

use Exception;

class OffsetAlreadyPassedException extends Exception
{
public static function make(): static
{
return new static("You can't paginate if you use a offset.");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php declare(strict_types=1);
<?php

namespace ProtoneMedia\LaravelCrossEloquentSearch;
declare(strict_types=1);

namespace ProtoneMedia\LaravelCrossEloquentSearch\Exceptions;

use Exception;

class OrderByRelevanceException extends Exception
{
public static function new()
public static function make(): static
{
return new static("You can't order by relevance if you're searching through nested relations.");
}
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/PaginateAlreadyPassedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace ProtoneMedia\LaravelCrossEloquentSearch\Exceptions;

use Exception;

class PaginateAlreadyPassedException extends Exception
{
public static function make(string $place): static
{
return new static("You can't set $place if you use a paginate or simplePaginate.");
}
}
4 changes: 3 additions & 1 deletion src/ModelToSearchThrough.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace ProtoneMedia\LaravelCrossEloquentSearch;

Expand Down
40 changes: 24 additions & 16 deletions src/Search.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace ProtoneMedia\LaravelCrossEloquentSearch;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Facade;

/**
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher new()
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher orderByAsc()
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher orderByDesc()
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher dontParseTerm()
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher includeModelType()
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher beginWithWildcard(bool $state)
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher endWithWildcard(bool $state)
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher soundsLike(bool $state)
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher add($query, $columns, string $orderByColumn = null)
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher when($value, callable $callback = null, callable $default = null)
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher addMany($queries)
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher paginate($perPage = 15, $pageName = 'page', $page = null)
* @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher simplePaginate($perPage = 15, $pageName = 'page', $page = null)
* @method static \Illuminate\Support\Collection parseTerms(string $terms, callable $callback = null)
* @method static \Illuminate\Database\Eloquent\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator get(string $terms = null)
* @method static Searcher new()
* @method static Searcher orderByAsc()
* @method static Searcher orderByDesc()
* @method static Searcher dontParseTerm()
* @method static Searcher includeModelType()
* @method static Searcher beginWithWildcard(bool $state)
* @method static Searcher endWithWildcard(bool $state)
* @method static Searcher soundsLike(bool $state)
* @method static Searcher offset(int $offset)
* @method static Searcher limit(int $limit)
* @method static Searcher when($value, callable $callback = null, callable $default = null)
* @method static Searcher add(Builder|string $query, iterable|string|Collection $columns = null, null|string $orderByColumn = null)
* @method static Searcher addMany(iterable $queries)
* @method static Searcher paginate(int $perPage = 15, string $pageName = 'page', null|int $page = null)
* @method static Searcher simplePaginate(int $perPage = 15, string $pageName = 'page', null|int $page = null)
* @method static Collection parseTerms(string $terms, null|callable $callback = null)
* @method static EloquentCollection|LengthAwarePaginator get(null|string $terms = null)
*
* @see \ProtoneMedia\LaravelCrossEloquentSearch\Searcher
*/
Expand Down
6 changes: 4 additions & 2 deletions src/SearchFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace ProtoneMedia\LaravelCrossEloquentSearch;

Expand All @@ -15,7 +17,7 @@ class SearchFactory
*/
public function new(): Searcher
{
return new Searcher;
return new Searcher();
}

/**
Expand Down
Loading