Package Data | |
---|---|
Maintainer Username: | ra3oul |
Maintainer Contact: | ra3oul.abdollahi@gmail.com (rasoul abdollahi) |
Package Create Date: | 2016-06-15 |
Package Last Update: | 2016-06-15 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-02-09 15:23:37 |
Package Statistics | |
---|---|
Total Downloads: | 173 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 15 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 0 |
using repository pattern in laravel with a great base repository
Installation
Methods
Usage
Execute the following command to get the latest version of the package:
composer require ra3oul/eloquent-abstract-repository -dev
In your config/app.php
add
ra3oul\EloquentAbstractRepository\EloquentAbstractRepositoryServiceProvider::class
to the end of the providers
array:
'providers' => [
...
ra3oul\EloquentAbstractRepository\EloquentAbstractRepositoryServiceProvider::class,
],
Create your model normally, but it is important to define the attributes that can be filled from the input form data.
namespace App;
class Article extends Eloquent { // can be any other class name
protected $fillable = [
'name',
'author',
...
];
...
}
namespace App;
use Foo ;
use ra3oul\EloquentAbstractRepository\repository\RepositoryInterface;
interface ArticleRepositoryInterface extends RepositoryInterface
{
}
namespace App;
use Foo ;
class ArticleRepository extends AbstractEloquentRepository implements ArticleRepositoryInterface
public function __construct(Foo $model)
{
parent::__construct($model);
}
}
in order to bind interfaces to repository classes we should create a simple service provider to bind them
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
protected function registeredRepositories()
{
// 'Repository Interface' => 'Implementation'
return [
'\App\ArticleRepositoryInterface' =>
'\App\ArticleRepository',
// you should add other interfaces and their implemented classes below !
];
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$repos = $this->registeredRepositories();
foreach ($repos as $interface => $implemented) {
$this->app->bind($interface, $implemented);
}
}
namespace App\Http\Controllers;
use App\ArticleRepositoryInterface;
class ArticlesController extends BaseController {
/**
* @var ArticleRepository
*/
protected $repository;
public function __construct(FooRepositoryInterface $repository){
$this->repository = $repository;
}
....
}
Find all results in Repository
$articles = $this->repository->findAll();
Find all results in Repository with pagination
$aritcles = $this->repository->columns([*])->paginate($limit=10);
Find by result by id
$articles = $this->repository->findOneById($id);
Showing only specific attributes of the model
$article = $this->repository->columns(['id', 'state_id'])->findOneById($id);
Loading the Model relationships
$article = $this->repository->with(['state'])->findOneById($id);
Find by result by field name
$articles = $this->repository->findOneBy('country_id','15');
Find by result by field
$articles = $this->repository->findManyBy('name','rasoul');
Find by result by multiple values in id
$articles = $this->repository->findManyByIds([1,2,3,4,5]);
Create new entry in Repository
$article = $this->repository->create( Input::all() );
Update entry in Repository
$article = $this->repository->updateOneById( $id , Input::all());
Delete entry in Repository
$this->repository->deleteOneById($id)