phpvms/app/Repositories/BaseRepository.php

66 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Repositories;
use Illuminate\Validation\Validator;
2017-08-25 02:03:10 +08:00
abstract class BaseRepository extends \Prettus\Repository\Eloquent\BaseRepository {
/**
* @param $id
* @param array $columns
* @return mixed|void
*/
2017-11-01 09:29:40 +08:00
public function findWithoutFail($id, $columns = ['*'])
{
try {
return $this->find($id, $columns);
2018-01-09 06:42:08 +08:00
} catch (\Exception $e) {
2017-11-01 09:29:40 +08:00
return;
}
}
/**
* @param $values
* @return bool
*/
2017-11-01 09:29:40 +08:00
public function validate($values)
{
$validator = Validator::make(
$values,
$this->model()->rules
);
if($validator->fails()) {
return $validator->messages();
}
return true;
}
2017-12-02 01:25:58 +08:00
/**
* Return N most recent items, sorted by created_at
* @param int $count
* @param string $sort_by created_at (default) or updated_at
* @return mixed
*/
public function recent($count = 5, $sort_by = 'created_at')
{
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
* @param $where
* @param $sort_by
* @param $order_by
* @return $this
*/
public function whereOrder($where, $sort_by, $order_by)
{
return $this->scopeQuery(function($query) use ($where, $sort_by, $order_by) {
return $query->where($where)->orderBy($sort_by, $order_by);
});
}
}