Download the PHP package mattb-it/larepo without Composer
On this page you can find all versions of the php package mattb-it/larepo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mattb-it/larepo
More information about mattb-it/larepo
Files in mattb-it/larepo
Package larepo
Short Description A Laravel repository pattern implementation
License MIT
Homepage https://github.com/mattb-it/larepo
Informations about the package larepo
Larepo - Laravel Repository
đź“„ Online Documentation
Introduction
This package is designed to help developers organize their code by offering a simplified approach to implementing the Repository Pattern in Laravel. While it doesn’t follow the pattern in its purest form, it provides a practical solution that aligns well with Laravel’s architecture. Eloquent, being as powerful as it is, handles many database interactions behind the scenes, making a strict Repository Pattern not really useful. Instead, this package focuses on helping you structure your code more effectively without adding complexity.
With this package, you’ll find it easier to apply SOLID principles, search through your codebase for operations like querying, deleting, or saving models, and simplify the way you manage models by introducing DTOs (Data Transfer Objects). It’s especially useful for intermediate developers looking to build clean, maintainable applications while keeping things simple.
Getting started
Installation
To install Larepo via Composer, run the following command:
Generate repository
Laravel projects typically include a User
model by default. Let's generate a UserRepository
using Larepo's artisan command:
This command will create a UserRepository
class in app/Repositories/UserRepository.php
:
Generate model DTO
After generating the UserRepository
, the next step is to create a corresponding DTO (Data Transfer Object) for the User
model. The DTO is necessary for using the save method.
This command generates the UserDTO
class in app/DTO/Models/UserDTO.php
:
Now that you've generated the UserDTO
, you can populate it with the model's attributes. This will make saving the model easier. Here's an example DTO for the User
model:
Attribute enum
The Attribute
enum allows you to control whether a model's attribute should be updated or left unchanged. This is particularly useful when handling user input. For instance, if the request doesn't contain the name field, you can pass Attribute::UNDEFINED
to signal that the attribute should not be modified, rather than setting it to null.
Here's an example that demonstrates how this works:
In this example, if the name
parameter is absent from the request, Attribute::UNDEFINED
is passed. This tells the save
method that the name
field should not be updated. Without this enum, passing a missing parameter could inadvertently set the field to null
.
Methods
Larepo provides several methods to simplify common operations like querying, saving, and deleting models.
find
The find
method retrieves a specific model by its primary key (usually id
) or by another attribute:
If the model is not found, the method returns null
.
findOrFail
Similar to find, but throws a ModelNotFoundException
if the model is not found:
all
The all method retrieves all models from the database:
query
The query
method allows you to create a query builder instance, which you can use to apply conditions:
delete
The delete
method deletes the specified model instance from the database. It returns a bool
indicating success or failure:
save
The save
method creates or updates a model using a DTO that implements ModelDTOInterface
. Before using this method, ensure that you've generated the model's DTO by running the php artisan larepo:make:dto User
command.
You can also execute this method without model parameter to create a new model:
This method returns the created/updated model instance.
All versions of larepo with dependencies
illuminate/database Version ^11.27
illuminate/support Version ^11.27
illuminate/console Version ^11.27