Skip to content

Instantly share code, notes, and snippets.

@codyphobe
Forked from driesvints/SearchThreads.php
Created July 20, 2017 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyphobe/09cb94d8f106f61abc318ec446c61a6b to your computer and use it in GitHub Desktop.
Save codyphobe/09cb94d8f106f61abc318ec446c61a6b to your computer and use it in GitHub Desktop.
Invokable query object
<?php
$searchThreads = app(\App\Queries\SearchThreads::class);
// As a default value.
$results = array_get($someArray, 'foo', $searchThreads)
<?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 static function get(string $keyword, int $perPage = 20): Paginator
{
return Thread::feedQuery()
->where('threads.subject', 'LIKE', "%$keyword%")
->orWhere('threads.body', 'LIKE', "%$keyword%")
->paginate($perPage);
}
}
<?php
namespace App\Http\Controllers\Forum;
use App\Queries\SearchThreads;
use App\Http\Middleware\RedirectIfUnconfirmed;
class ThreadsController extends Controller
{
public function overview(SearchThreads $searchThreads)
{
$threads = $searchThreads(request('search'));
return view('forum.overview', compact('threads'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment