2017-06-11 07:27:19 +08:00
|
|
|
<?php
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
namespace App\Contracts;
|
2017-06-11 07:27:19 +08:00
|
|
|
|
|
|
|
use Illuminate\Validation\Validator;
|
|
|
|
|
2018-03-20 10:01:32 +08:00
|
|
|
/**
|
|
|
|
* Class Repository
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
abstract class Repository extends \Prettus\Repository\Eloquent\BaseRepository
|
|
|
|
{
|
2017-12-02 00:53:33 +08:00
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2017-12-02 00:53:33 +08:00
|
|
|
* @param array $columns
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @return mixed|null
|
2017-12-02 00:53:33 +08:00
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
public function findWithoutFail($id, array $columns = ['*'])
|
2017-11-01 09:29:40 +08:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->find($id, $columns);
|
2018-01-09 06:42:08 +08:00
|
|
|
} catch (\Exception $e) {
|
2018-08-27 00:40:04 +08:00
|
|
|
return;
|
2017-11-01 09:29:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 00:53:33 +08:00
|
|
|
/**
|
|
|
|
* @param $values
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-02 00:53:33 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-11-01 09:29:40 +08:00
|
|
|
public function validate($values)
|
|
|
|
{
|
2017-06-11 07:27:19 +08:00
|
|
|
$validator = Validator::make(
|
|
|
|
$values,
|
|
|
|
$this->model()->rules
|
|
|
|
);
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($validator->fails()) {
|
2017-06-11 07:27:19 +08:00
|
|
|
return $validator->messages();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-02 01:25:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return N most recent items, sorted by created_at
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param int $count
|
2017-12-02 01:25:58 +08:00
|
|
|
* @param string $sort_by created_at (default) or updated_at
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-02 01:25:58 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-03-01 05:06:57 +08:00
|
|
|
public function recent($count = null, $sort_by = 'created_at')
|
2017-12-02 01:25:58 +08:00
|
|
|
{
|
|
|
|
return $this->orderBy($sort_by, 'desc')->paginate($count);
|
|
|
|
}
|
2018-01-06 05:30:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find records with a WHERE clause but also sort them
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-06 05:30:35 +08:00
|
|
|
* @param $where
|
|
|
|
* @param $sort_by
|
|
|
|
* @param $order_by
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-06 05:30:35 +08:00
|
|
|
* @return $this
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function whereOrder($where, $sort_by, $order_by = 'asc')
|
2018-01-06 05:30:35 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
return $this->scopeQuery(function ($query) use ($where, $sort_by, $order_by) {
|
2018-03-03 07:29:11 +08:00
|
|
|
$q = $query->where($where);
|
2018-08-27 00:40:04 +08:00
|
|
|
// See if there are multi-column sorts
|
2018-03-20 09:50:40 +08:00
|
|
|
if (\is_array($sort_by)) {
|
|
|
|
foreach ($sort_by as $key => $sort) {
|
2018-03-03 07:29:11 +08:00
|
|
|
$q = $q->orderBy($key, $sort);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$q = $q->orderBy($sort_by, $order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $q;
|
2018-01-06 05:30:35 +08:00
|
|
|
});
|
|
|
|
}
|
2018-04-17 05:33:41 +08:00
|
|
|
|
|
|
|
/**
|
2018-04-17 05:46:51 +08:00
|
|
|
* Find records where values don't match a list but sort the rest
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-17 05:33:41 +08:00
|
|
|
* @param string $col
|
|
|
|
* @param array $values
|
|
|
|
* @param string $sort_by
|
|
|
|
* @param string $order_by
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-17 05:33:41 +08:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function whereNotInOrder($col, $values, $sort_by, $order_by = 'asc')
|
|
|
|
{
|
|
|
|
return $this->scopeQuery(function ($query) use ($col, $values, $sort_by, $order_by) {
|
|
|
|
$q = $query->whereNotIn($col, $values);
|
2018-08-27 00:40:04 +08:00
|
|
|
// See if there are multi-column sorts
|
2018-04-17 05:33:41 +08:00
|
|
|
if (\is_array($sort_by)) {
|
|
|
|
foreach ($sort_by as $key => $sort) {
|
|
|
|
$q = $q->orderBy($key, $sort);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$q = $q->orderBy($sort_by, $order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $q;
|
|
|
|
});
|
|
|
|
}
|
2019-09-13 23:21:40 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve all data of repository, paginated. Added in extra parameter to read from the
|
|
|
|
* request which page it should be on
|
|
|
|
*
|
|
|
|
* @param null $limit
|
|
|
|
* @param array $columns
|
|
|
|
* @param string $method
|
|
|
|
*
|
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function paginate($limit = null, $columns = ['*'], $method = 'paginate')
|
|
|
|
{
|
|
|
|
$this->applyCriteria();
|
|
|
|
$this->applyScope();
|
|
|
|
|
|
|
|
$max = config('repository.pagination.limit', 50);
|
|
|
|
$limit = (int) ($limit ?? request()->query('limit') ?? $max);
|
|
|
|
$page = (int) request()->query('page', 1);
|
|
|
|
|
|
|
|
$results = $this->model->{$method}($limit, $columns, 'page', $page);
|
|
|
|
$results->appends(app('request')->query());
|
|
|
|
$this->resetModel();
|
|
|
|
|
|
|
|
return $this->parserResult($results);
|
|
|
|
}
|
2017-06-11 07:27:19 +08:00
|
|
|
}
|