Created
May 26, 2023 10:47
-
-
Save filipfilipovich/b1a4f1b926f7cceded80c300cfb0ee60 to your computer and use it in GitHub Desktop.
BookRepository class
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 | |
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