72 lines
1.3 KiB
PHP
72 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Api;
|
|
|
|
use App\Repositories\BaseRepositoryInterface;
|
|
|
|
abstract class BaseApiRepository implements BaseRepositoryInterface
|
|
{
|
|
protected $model;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->setModel();
|
|
}
|
|
|
|
abstract public function getModel();
|
|
|
|
protected function setModel()
|
|
{
|
|
$this->model = app()->make($this->getModel());
|
|
return $this;
|
|
}
|
|
|
|
public function create(array $data = [])
|
|
{
|
|
return $this->model->create($data);
|
|
}
|
|
|
|
public function getAll()
|
|
{
|
|
return $this->model->all();
|
|
}
|
|
|
|
public function find($id)
|
|
{
|
|
return $this->model->find($id);
|
|
}
|
|
|
|
public function insert(array $data = [])
|
|
{
|
|
return $this->model->insert($data);
|
|
}
|
|
|
|
public function update(int $id = 0, array $data = [])
|
|
{
|
|
$result = $this->find($id);
|
|
|
|
if ($result) {
|
|
$result->update($data);
|
|
return $result;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
$result = $this->find($id);
|
|
if ($result) {
|
|
$result->delete();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function getCount()
|
|
{
|
|
return $this->model->count('id');
|
|
}
|
|
}
|