Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active May 7, 2019 04:24
Show Gist options
  • Save afiqiqmal/1dc54cfec542593ad20e80f45b780e16 to your computer and use it in GitHub Desktop.
Save afiqiqmal/1dc54cfec542593ad20e80f45b780e16 to your computer and use it in GitHub Desktop.
Use for extend the others Model Repository Class

Base Repository Laravel

This is use for extend the others Model Repository Class

Usage

class UserRepository extends BaseRepository {
    public function __construct(User $user)
    {
        parent::__construct($user);
    }
}
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 19/07/2018
* Time: 2:21 PM
*/
namespace App\Repository;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class BaseRepository implements RepositoryInterface
{
protected $model;
public function __construct(Model $model)
{
$this->model = $model;
}
public function getQuery()
{
return $this->model->query();
}
public function first()
{
return $this->getQuery()->first();
}
public function all()
{
return $this->getQuery()->get();
}
public function count()
{
return $this->getQuery()->count();
}
public function paginate($limit = 10)
{
return $this->getQuery()->paginate($limit);
}
public function find($id, $withTrash = false)
{
if ($withTrash) {
return $this->getQuery()->withTrashed()->find($id);
}
return $this->getQuery()->find($id);
}
public function where($column, $id, $first = false)
{
$query = $this->getQuery()->where($column, $id);
return ($first) ? $query->first() : $query->get();
}
public function create(array $request)
{
return $this->getQuery()->create($request);
}
public function with($relation)
{
return $this->getQuery()->with($relation);
}
public function withRelation(array $relation)
{
return $this->getQuery()->with($relation)->get();
}
public function update($id, array $request, $withTrash = false)
{
if ($withTrash) {
$app = $this->getQuery()->withTrashed()->find($id);
} else {
$app = $this->getQuery()->find($id);
}
$app->update($request);
return $app;
}
public function updateBySlug($key_slug, array $request, $withTrash = false)
{
if ($withTrash) {
$app =$this->getQuery()->withTrashed()->findBySlug($key_slug);
} else {
$app = $this->getQuery()->findBySlug($key_slug);
}
$app->update($request);
return $app;
}
public function findBySlug($key_slug, $withTrash = false, $columns = ['*'])
{
if ($withTrash) {
return $this->getQuery()->withTrashed()->findBySlug($key_slug, $columns);
}
return $this->getQuery()->findBySlug($key_slug, $columns);
}
public function delete($id)
{
return $this->getQuery()->find($id)->delete();
}
}
<?php
interface RepositoryInterface
{
public function getQuery();
public function first();
public function all();
public function count();
public function paginate($limit = 10);
public function find($id, $withTrash = false);
public function where($column, $id, $first = false);
public function create(array $request);
public function update($id, array $request, $withTrash = false);
public function updateBySlug($key_slug, array $request, $withTrash = false);
public function findBySlug($key_slug, $withTrash = false, $columns = ['*']);
public function delete($id);
public function with($relation);
public function withRelation(array $relation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment