Download the PHP package imjonos/laravel-base-repository without Composer
On this page you can find all versions of the php package imjonos/laravel-base-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download imjonos/laravel-base-repository
More information about imjonos/laravel-base-repository
Files in imjonos/laravel-base-repository
Package laravel-base-repository
Short Description Laravel base repository
License MIT
Homepage https://github.com/imjonos/laravel-base-repository
Informations about the package laravel-base-repository
๐ฆ Laravel Base Repository
A generic base repository class for Laravel projects that provides a clean and consistent way to interact with Eloquent models. It simplifies CRUD operations and makes your code more maintainable, testable, and scalable.
๐งฉ Overview
This package provides an abstract EloquentRepository class that implements the EloquentRepositoryInterface. It wraps common model interactions into reusable methods, making it easier to manage data access logic in your Laravel applications.
๐ Installation
Install the package via Composer:
โ Usage
1. Create Your Repository Class
Create a new repository class that extends EloquentRepository and specifies the model class:
Note: If you use Laravel Scout with the
Searchabletrait on your model, the repository will automatically keep the search index in sync.
2. Use the Repository in a Controller or Service
Inject the repository and use its methods:
๐ง Available Methods
| Method | Description |
|---|---|
all() |
Get all records |
count() |
Count all records |
create(array $data) |
Create a new record |
update(int $id, array $data) |
Update a record by ID |
exists(int $id) |
Check if a record exists |
find(int $id) |
Find a record by ID (returns null if not found) |
delete(int $id) |
Delete a record by ID |
query() |
Return a query builder instance for custom queries |
insert(array $data) |
Insert multiple records (no events, no scout) |
upsert(array $values, array\| string $uniqueBy, ?array $update) |
Update or insert multiple records |
๐ Laravel Scout Support
The repository automatically handles Laravel Scout indexing when using the Searchable trait:
- After
upsert(): Makes records searchable - After
update(): Updates search index - After
delete(): Removes from search index
๐ Project Structure
๐ฆ Requirements
- PHP 8.0+
- Laravel 5.0+ - 13.x
๐งช Testing
You can easily mock the repository interface in your tests, which helps keep your application logic decoupled from the database and improves test coverage.
๐ Contributing
Please see contributing.md for details and a todolist.
๐ License
This package is open-sourced software licensed under the MIT license. Please see the license file for more information.