Download the PHP package salehhashemi/laravel-repository without Composer
On this page you can find all versions of the php package salehhashemi/laravel-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download salehhashemi/laravel-repository
More information about salehhashemi/laravel-repository
Files in salehhashemi/laravel-repository
Package laravel-repository
Short Description Implementing the repository pattern for Laravel projects.
License MIT
Homepage https://github.com/salehhashemi1992/laravel-repository
Informations about the package laravel-repository
This Laravel package simplifies and streamlines data access by implementing the repository pattern. It provides a powerful abstraction layer for your database interactions, enhanced with flexible filtering, searching, and criteria-based querying.
Features
- Repository Abstraction: Decouples data access logic from application code.
- Dynamic Filtering: Apply filters to refine database queries.
- Criteria-Based Filtering: Define reusable criteria for common query constraints.
- Search Functionality: Implement custom search logic based on user input.
- Modular Repository Interface: Enforces consistency and maintainability.
- Ease of Integration: Seamlessly integrates with Laravel applications.
- Development Tools: Automate repository, interface, and filter creation with the
make:repository
command.
Installation
To install the package, you can run the following command:
Development Tools
The package includes a helpful command to speed up your development workflow:
-
make:repository
This command automates the creation of the following components for a specified model:- Repository Class
- Repository Interface
- Filter Class
Usage Example:
or
Example Usage
Practical example of how to use this package to manage and interact with a Post model.
It includes examples of a custom repository, filter, criteria, and controller usage.
Post Repository
The PostRepository
class extends BaseEloquentRepository and demonstrates the use of the package's features for a Post model.
Post Repository Interface
The PostRepositoryInterface
defines the contract for our repository, ensuring consistency and clarity in method signatures.
Post Filter
PostFilter
demonstrates how to apply custom filters to the Post model, enhancing query flexibility.
Featured Post Criteria
The FeaturedPostCriteria
is an example of defining criteria for filtering, here focusing on featured posts.
Binding Interface to Implementation
It is crucial to bind the repository interface to its concrete implementation. This enables Laravel service container to automatically resolve and inject the appropriate repository instance wherever it's needed.
-
Create a Service Provider
-
Bind in the Service Provider
- Register the Service Provider: Add your
RepositoryServiceProvider
to the providers array inconfig/app.php
.
Repository Usage in controller
This example shows how to use the PostRepository
within a controller, integrating it seamlessly into a Laravel application workflow.
Available Methods
This package offers several methods:
findOne
Fetches a single model instance by primary key, or the first model matching applied criteria and relations.
findOneOrFail
Similar to findOne, but throws a ModelNotFoundException
if no result is found.
findAll
Retrieves a collection of all models matching the applied criteria, relations, and specified options.
findList
Gets a collection of model values keyed by a specified field, useful for dropdown lists or similar displays.
paginate
Returns a paginated collection of models based on applied criteria, relations, and specified page size.
addCriteria
Adds a new criteria instance to the repository for query filtering.
orderBy
Specifies ordering for the query results based on field and direction.
lockForUpdate
Applies a "lock for update" clause to the query for handling concurrent database access.
sharedLock
Applies a shared lock to the query, preventing modifications of selected rows.
Configuration
To publish the config file, run the following command:
After publishing, make sure to clear the config cache to apply your changes:
Then, you can adjust the pagination limit in the config/repository.php
Exceptions
InvalidArgumentException
Thrown if page size is invalid.ModelNotFoundException
Thrown if model not found.
Docker Setup
This project uses Docker for local development and testing. Make sure you have Docker and Docker Compose installed on your system before proceeding.
Build the Docker images
Start the services
To access the PHP container, you can use:
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- Saleh Hashemi
- IMahmood
- All Contributors
License
The MIT License (MIT). Please see License File for more information.