|
<?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(); |
|
} |
|
} |