Created
July 3, 2017 07:37
Star
You must be signed in to star a gist
Multiple method query objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$searchThreads = app(App\Queries\SearchThreads::class); | |
$allThreads = $searchThreads('foo'); | |
$paginatedThreads = $searchThreads->paginated('foo'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Queries; | |
use App\Models\Thread; | |
use Illuminate\Contracts\Pagination\Paginator; | |
class SearchThreads | |
{ | |
public function __construct() | |
{ | |
// gets resolved through IoC. | |
} | |
/** | |
* @return \App\Models\Thread[] | |
*/ | |
public function __invoke(string $keyword) | |
{ | |
return $this->base($keyword)->get(); | |
} | |
/** | |
* @return \App\Models\Thread[] | |
*/ | |
public function paginated(string $keyword, int $perPage = 20): Paginator | |
{ | |
return $this->base($keyword)->paginate($perPage); | |
} | |
private method base(string $keyword) | |
{ | |
return Thread::feedQuery() | |
->where('threads.subject', 'LIKE', "%$keyword%") | |
->orWhere('threads.body', 'LIKE', "%$keyword%"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment