Download the PHP package foothing/laravel-repository without Composer
On this page you can find all versions of the php package foothing/laravel-repository. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download foothing/laravel-repository
More information about foothing/laravel-repository
Files in foothing/laravel-repository
Package laravel-repository
Short Description This package provides an implementation of the repository pattern for Laravel 5.
License MIT
Informations about the package laravel-repository
Laravel repository
This package provides an implementation of the repository pattern for Laravel 5.
Composer installation
Basic usage
This package ships with a repository interface definition and
an Eloquent
implementation.
The EloquentRepository
is ready to use.
You are now ready to go:
If you want to extend the base implementation with
your custom methods all you need to do is extend the
base class and define a constructor with the Eloquent
model as the first argument.
Don't forget to call the superclass constructor in order to enable all the features that are delivered out of the box. You can also add additional dependencies while overriding the base constructor.
and you are all set. Examples:
Eager load relations
You can eager load relations using the with
method:
You can chain the with
method to all available methods:
You'll often want to eager load some relations by default. Let's assume you have an User model with a many-to-many relation with its Role models, and you want to fetch both the user info and the roles.
The Foothing\Resources\Resource
interface has three methods
which define the default Model behaviour:
- which relations to eager load by default for a findOne operation
- which relations to eager load by default for a findMany operation
- which fields to skip upon save
In this way, each time you will use your UserRepository the following would be the default behaviour:
This default behaviour will be ignored if you use the with
method explicitly.
The skipOnSave
method comes handy when dealing with JSON object, since
when you read a resource, then process it on the client side, then
send it back to the server, it will contain the relation properties.
This properties will be converted to stdClass PHP object breaking the
Eloquent save method.
Criteria
You can apply filters and order constraints using the CriteriaInterface
,
which also ships with an Eloquent
implementation.
Usage:
The repository provides shortcut methods you can use in your chains. The following example produces the same effect as the previous one.
Available filters:
You can use the in operator with a comma separated list of values or with an array of values.
Nested filters
It's also possible to query a Model relation. Assume your
Eloquent Model defines a children
relation like so:
In this case you'll be allowed to query children
values:
Keep in mind that the methods subject to criteria restrictions are all those fetching more than one record.
Attach and detach many-to-many
Given the following Model
:
you can use the attach()
and detach()
methods to attach and detach to / from relation.
Scopes
You can plug-in your Model scopes using the scope()
method.
Scopes only apply on read methods.
Global Scopes
You can define a global scope for each repository implementation, which might come handy to restrict records access globally.
Just set
in your repository. The global scope must match an Eloquent scope.
Note that if you use explicit scopes, i.e. $repository->scope('male')
,
your global scope will be ignored.
Repository API
RemoteQuery
More info coming soon.