Skip to content

Instantly share code, notes, and snippets.

@filipfilipovich
Created May 26, 2023 10:47
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 filipfilipovich/b1a4f1b926f7cceded80c300cfb0ee60 to your computer and use it in GitHub Desktop.
Save filipfilipovich/b1a4f1b926f7cceded80c300cfb0ee60 to your computer and use it in GitHub Desktop.
BookRepository class
<?php
declare(strict_types=1);
namespace App\Repositories;
use App\Models\Book;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
class BookRepository
{
public function findBy(array $parameters): Collection
{
$query = Book::query();
foreach ($parameters as $attribute => $value) {
$query->where($attribute, $value);
}
return $query->get();
}
public function filterBy(array $parameters): Collection
{
$query = Book::query();
$query->where(function (Builder $subQuery) use ($parameters) {
foreach ($parameters as $attribute => $value) {
$subQuery->orWhere($attribute, 'like', '%' . $value . '%');
}
});
return $query->get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment