Skip to content

Instantly share code, notes, and snippets.

@einnar82
Created November 21, 2021 07:25
Show Gist options
  • Save einnar82/50aac7594942199e684d3e5883c50cc8 to your computer and use it in GitHub Desktop.
Save einnar82/50aac7594942199e684d3e5883c50cc8 to your computer and use it in GitHub Desktop.
Sample Repository Pattern for Laravel
<?php
namespace App\Repositories;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\Paginator;
abstract class BaseRepository
{
protected Model $model;
public function __construct(Model $model)
{
$this->model = $model;
}
/**
* @param array $attributes
* @return Model
*/
public function create(array $attributes): ?Model
{
return $this->model->create($attributes);
}
/**
* @param array $attributes
* @param Model $model
* @return Model
*/
public function update(array $attributes, Model $model): ?Model
{
return tap($model)->update($attributes);
}
/**
* @return Paginator
*/
public function all(): ?Paginator
{
return $this->model->orderBy('id', 'desc')->simplePaginate();
}
/**
* @param Model $model
* @return Model
*/
public function find(Model $model): ?Model
{
return $model;
}
/**
* @param Model $model
* @return bool
*/
public function deleteOne(Model $model): ?bool
{
return $model->delete();
}
}
<?php
namespace App\Contracts;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\Paginator;
interface DatabaseRepositoryContract
{
/**
* @param array $attributes
* @return Model
*/
public function create(array $attributes): ?Model;
/**
* @param array $attributes
* @param Model $model
* @return Model
*/
public function update(array $attributes, Model $model): ?Model;
/**
* @return Paginator
*/
public function all(): ?Paginator;
/**
* @param Model $model
* @return Model
*/
public function find(Model $model): ?Model;
/**
* @param Model $model
* @return bool
*/
public function deleteOne(Model $model): ?bool;
}
<?php
namespace App\Providers;
use App\Modules\Transfer\Contracts\TransferRepositoryContract;
use App\Modules\Transfer\Models\Transfer;
use App\Modules\Transfer\Repositories\TransferRepository;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->registerRepositories();
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
public function registerRepositories()
{
$this->app->instance(TransferRepositoryContract::class, new TransferRepository(new Transfer()));
}
}
<?php
namespace app\Modules\Transfer\Repositories;
use App\Modules\Transfer\Contracts\TransferRepositoryContract;
use App\Modules\Transfer\Models\Transfer;
use App\Repositories\BaseRepository;
class TransferRepository extends BaseRepository implements TransferRepositoryContract
{
public function __construct(Transfer $model)
{
parent::__construct($model);
}
}
<?php
namespace App\Modules\Transfer\Contracts;
use App\Contracts\DatabaseRepositoryContract;
interface TransferRepositoryContract extends DatabaseRepositoryContract
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment