laravel-repository maintained by ark4ne
Description
Laravel Repository
Author
Last update
2021/06/06 22:12
(dev-master)
License
Downloads
18
Tags
laravel-repository
Laravel Repository implementation
Usage
use Illuminate\Database\Eloquent\Model;
class Pet extends Model {
// ...
}
use Ark4ne\Repositories\Contracts\RepositoryContract;
interface PetRepositoryContract extends RepositoryContract {
//
}
use Ark4ne\Repositories\Eloquent\Repository;
class PetRepository extends Repository implements PetRepositoryContract {
protected function getModel() : Pet {
return new Pet;
}
}
// RepositoryServiceProvider.php
public function register() {
$this->app->bind(PetRepositoryContract::class, PetRepository::class);
}
// PetController.php
class PetController extends Controller {
private $repository;
public function __construct(PetRepositoryContract $repository) {
$this->repository = $repository;
}
public function store(PetStoreRequest $request) {
$data = $request->validated();
$pet = $this->repository->store($data);
return new PetResource($pet);
}
}
Methods
count
Count models matching $criteria.
count(array<string, null|int|string|Closure> $criteria): int
all
Return all models matching $criteria.
all(): \Illuminate\Support\Collection
paginate
Return a paginate list of model matching $criteria.
paginate(array<string, null|int|string|Closure> $criteria, int|null $per_page): \Illuminate\Contracts\Pagination\LengthAwarePaginator
find
Return a model by his id.
find(int|string $id): \Illuminate\Database\Eloquent\Model
findBy
Return a model where $field match the given value.
findBy(string $field, mixed $value): \Illuminate\Database\Eloquent\Model
findByMany
Return a model matching $criteria.
findByMany(array<string, null|int|string|Closure> $criteria): \Illuminate\Database\Eloquent\Model
getWhere
Return a collection of model where $field match the given value.
getWhere(string $field, mixed $value): \Illuminate\Support\Collection
getWhereMany
Return a collection of model matching $criteria.
getWhereMany(array<string, null|int|string|Closure> $criteria): \Illuminate\Support\Collection
store
Create and return a new model.
store(array<string, mixed> $data): \Illuminate\Database\Eloquent\Model
update
Update an existing model
update(int|string $id, array<string, mixed> $data): \Illuminate\Database\Eloquent\Model
delete
Delete an existing model
delete(int|string $id): bool
withCriteria
Add criteria
withCriteria(array<string, null|int|string|Closure> $criteria): self
withRelationships
Add relationships that should be eager loaded.
withRelationships(array<string> $relationships): self