Download the PHP package adamkhoa03/eloquent-repository without Composer
On this page you can find all versions of the php package adamkhoa03/eloquent-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adamkhoa03/eloquent-repository
More information about adamkhoa03/eloquent-repository
Files in adamkhoa03/eloquent-repository
Package eloquent-repository
Short Description Eloquent Repository for Laravel
License MIT
Homepage https://github.com/adamkhoa03/eloquent-repository
Informations about the package eloquent-repository
Eloquent Repository for Laravel
Eloquent Repository package for Laravel created with total "repository pattern" in-mind.
Requirements
Version requirement and compatibility with Laravel
Version ^1.0 - Only support laravel 8.
Installation
You can install the package via composer:
Usage
Create a repository class and extend Adamkhoa03\EloquentRepository\EloquentRepository
abstract class.
Repository class which extends EloquentRepository
must implement entity
method. When using Eloquent models it's enough to return model's full namespace from the method.
Package also comes with console command that can create repository class. Pass repository class name to make:repository
command to generate it.
This will create repository class inside Repositories
folder in your app's autoload directory.
You can also pass Eloquent model namespace to make:repository
command to automatically apply add that model to repository.
This will create UserRepository
class and apply User
model as entity to it.
You can use Laravel's container to inject UserRepository
repository.
You can also skip creating dedicated repository class altogether,
instead inject Adamkhoa03\EloquentRepository\EloquentRepository
and set Eloquent model entity dynamically.
Available methods
Eloquent Repository class offers has many familiar shortcut methods from Eloquent.
Create a model:
Creates a user with given parameters and returns created model instance.
Return all models:
Finds and returns all users with all allowed columns.
Return all models with listed columns:
Finds and returns all users with listed columns. You can skip list of columns, method will act same as all()
.
Paginate and return all models with given "per page" value:
Paginates all users with given "per page" value and returns paginated result.
Find a user with primary key:
Finds user with given primary key and returns model instance. If model is not available Illuminate\Database\Eloquent\ModelNotFoundException
exception will be thrown.
Return all users with given "where" statement:
Returns all users where first_name
is "John".
You can also pass multiple multiple "where" statements in first parameter and skip second parameter.
Returns all users where first_name
is "John" and last_name
is "Doe".
Return first user with given "where" statement:
Returns first user where first_name
is "John".
You can also pass multiple multiple "where" statements in first parameter and skip second parameter.
Returns first user where first_name
is "John" and last_name
is "Doe".
Return all users with given "whereIn" statement:
Returns all users where first_name
is "John", "Jane" or "Dave".
Return first user with given "whereIn" statement:
Returns first user where first_name
is "John", "Jane" or "Dave".
Update a model with given properties:
Updates $user
model's first_name
to "Dave" and returns updated instance.
Find a model using primary key and update with given properties:
Finds a user with given primary key, updates first_name
to "Dave" and returns updated instance. If model is not available Illuminate\Database\Eloquent\ModelNotFoundException
exception will be thrown.
Delete a model:
Deletes $user
model.
Find a model using primary key and delete:
Finds a user with given primary key and deletes. If model is not available Illuminate\Database\Eloquent\ModelNotFoundException
exception will be thrown.
Find a "soft deleted" model:
Finds a "soft deleted" user with given primary key. If model is not using "soft delete" feature method will throw BadMethodCallException
exception. If model is not available Illuminate\Database\Eloquent\ModelNotFoundException
exception will be thrown.
Restore a "soft deleted" model:
Restores a "soft deleted" a $user
model. If model is not using "soft delete" feature method will throw BadMethodCallException
exception.
Find a "soft deleted" model using primary key and restore:
Finds a "soft deleted" user with given primary key and restores. If model is not using "soft delete" feature method will throw BadMethodCallException
exception. If model is not available Illuminate\Database\Eloquent\ModelNotFoundException
exception will be thrown.
Criteria
Package uses "criteria" for creating flexible queries. To use criteria chain withCriteria()
method to repository. List of available criteria:
EagerLoad:
Use Adamkhoa03\EloquentRepository\Repository\Eloquent\Criteria\EagerLoad
to eager load relationships with query.
This will return all users with posts
and country
relationships eager loaded.
Scope:
Use Adamkhoa03\EloquentRepository\Repository\Eloquent\Criteria\Scope
to apply eloquent query scopes.
This will apply active
and active
scopes to query and return all available users.
OrderBy:
Use Adamkhoa03\EloquentRepository\Repository\Eloquent\Criteria\OrderBy
to order results with specified column and type.
This will return order users by ascending username
column and return all of them.
Latest:
Use Adamkhoa03\EloquentRepository\Repository\Eloquent\Criteria\Latest
to order results with specified column and in descending order.
This will return order users by descending username
column and return all of them.
You can apply multiple scopes to repository at the same time.
You can create your own criteria classes and use them with withCriteria()
method.
Every criteria class must implement Adamkhoa03\EloquentRepository\Repository\Criteria\Criterion
interface. This interface requires having apply($entity)
method:
Caching
Repository also supports caching models. To enable caching implement Adamkhoa03\EloquentRepository\Repository\Contracts\Cacheable
interface to your repository:
Once implemented, all
and find()
methods will cache results.
Repository will empty the cache automatically when update()
, findAndUpdate()
, delete()
and findAndDelete()
methods used.
You can implement cacheKey()
method in your repository to set cache key. Default is model's table name.
You can set cache time-to-live with $cacheTTL
property. By default it is set to 3600 (1 hour).
Alternatively you can implement cacheTTL()
method in your repository to return cache time-to-live value.
Repository will ignore $cacheTTL
property value when cacheTTL()
method is implemented.
You can implement invalidateCache($model)
method in your repository to change cache invalidation logic when update()
, findAndUpdate()
, delete()
, findAndDelete()
methods being used.
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
All versions of eloquent-repository with dependencies
illuminate/container Version ^6.0|^7.0|^8.0
illuminate/console Version ^6.0|^7.0|^8.0
illuminate/contracts Version ^6.0|^7.0|^8.0
illuminate/database Version ^6.0|^7.0|^8.0
illuminate/support Version ^6.0|^7.0|^8.0