Download the PHP package nejcc/laravel-querylayer without Composer
On this page you can find all versions of the php package nejcc/laravel-querylayer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-querylayer
Laravel QueryLayer
A powerful and flexible repository pattern implementation for Laravel applications. This package provides a clean and maintainable way to handle database operations in your Laravel projects, with built-in singleton pattern support for efficient repository management.
Features
- ๐ Quick repository creation for any Eloquent model
- ๐ Full CRUD operations out of the box
- ๐ฆ Type-safe with PHP 8.0+ features and generics support
- ๐ฏ Query builder access for complex queries
- ๐ฑ Pagination support
- ๐ ๏ธ Extensible base repository for custom implementations
- ๐ก IDE-friendly with comprehensive PHPDoc annotations
- ๐ Singleton pattern implementation for efficient resource usage
- ๐๏ธ Soft delete support for models using SoftDeletes
- ๐ Database transaction support for reliable operations
- ๐ Eager loading support for optimized relationship queries
- ๐งน Automatic query scope reset for cleaner code
Requirements
- PHP 8.0 or higher
- Laravel 8.0 or higher
Installation
You can install the package via composer:
The package will automatically register its service provider.
Quick Start
Using the Facade
The simplest way to create a repository for any model:
Creating Custom Repositories
For more complex scenarios, extend the BaseRepository:
Available Methods
The repository provides a comprehensive set of methods for database operations:
| Method | Description |
|---|---|
all() |
Get all records (use with caution for large datasets) |
get() |
Get records for the current query |
paginate(?int $perPage = null) |
Get paginated results |
find(int|string $id) |
Find a record by ID |
findBy(string $column, mixed $value) |
Find a record by column value |
where(array $conditions) |
Add where conditions to the query |
create(array $data) |
Create a new record |
update(int|string $id, array $data) |
Update a record |
delete(int|string $id) |
Delete a record |
query() |
Get the query builder instance |
db() |
Get the raw DB query builder instance |
with(string\|array $relations) |
Eager load relationships |
withTrashed() |
Include soft deleted records |
onlyTrashed() |
Get only soft deleted records |
restore(int\|string $id) |
Restore a soft deleted record |
forceDelete(int\|string $id) |
Permanently delete a record |
transaction(callable $callback) |
Execute operations in a transaction |
createOrFail(array $data) |
Create a record in a transaction |
updateOrFail(int\|string $id, array $data) |
Update a record in a transaction |
reset() |
Reset query scopes and eager loading |
Method Chaining
The repository supports fluent method chaining for building complex queries:
Singleton Pattern
The package implements the singleton pattern for repository instances. This means:
- Each model class gets a single repository instance
- Subsequent calls to
LaravelQuerylayer::repository()with the same model return the same instance - This helps reduce memory usage and improves performance
- Perfect for dependency injection and service container usage
Soft Deletes
If your model uses Laravel's SoftDeletes trait, the repository automatically supports:
- Regular queries will exclude soft deleted records
- Use
withTrashed()to include soft deleted records - Use
onlyTrashed()to get only soft deleted records - Restore soft deleted records with
restore(id) - Permanently delete with
forceDelete(id)
Transactions
For data integrity, the repository provides transaction support:
- Wrap multiple operations in
transaction(function() { ... }) - Use
createOrFail()to ensure a record is created or transaction fails - Use
updateOrFail()to ensure an update succeeds or transaction fails
Eager Loading
Optimize database queries by eager loading relationships:
- Use
with('relation')to load a single relation - Use
with(['relation1', 'relation2'])to load multiple relations - Chain with other methods:
with('posts')->where(['active' => true])->get()
Direct Database Access with db()
In addition to the Eloquent query builder accessible via query(), the repository provides raw database access through the db() method:
The db() method provides:
- Direct access to Laravel's Query Builder for raw SQL operations
- Perfect for complex joins, aggregations, and statistical queries
- Useful for bulk operations that would be inefficient with Eloquent
- Access to advanced SQL features not available in Eloquent
When to use db() vs query():
- Use
query()when you need model features like relationships, accessors/mutators, and model events - Use
db()when you need raw performance, complex joins, or SQL-specific functionality - Choose
db()for statistical queries and reporting where you don't need full model instances
You can find more examples of using the db() method in the demo repositories:
- UserRepository - examples of grouping and counting
- PostRepository - examples of joins and complex filtering
- CommentRepository - examples of aggregation and bulk operations
Testing
The package includes a comprehensive test suite. Run the tests with:
Code Style
This package follows the PSR-12 coding standard. Format your code using Laravel Pint:
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Nejcc
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Model Interface
You can also use the repository functionality directly on your models by implementing the ModelInterface and using the HasRepository trait:
Now you can use repository methods directly on your model:
This approach gives you the flexibility to use repository methods either through the repository class or directly on your models.