48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Post;
|
|
|
|
use App\Models\Post;
|
|
use App\Repositories\BaseRepository;
|
|
|
|
class PostRepository extends BaseRepository implements PostRepositoryInterface
|
|
{
|
|
public function getModel()
|
|
{
|
|
return Post::class;
|
|
}
|
|
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
|
{
|
|
$model = $this->model->with($relationships)->select($select);
|
|
|
|
if (isset($data['status']) && ! empty($data['status']) && $data['status'] != 'full') {
|
|
$model = $model->where("status", match (strtolower($data['status'])) {
|
|
'published' => 1,
|
|
'draft' => 2,
|
|
'pending' => 0,
|
|
});
|
|
}
|
|
|
|
if (count($withCount) > 0) {
|
|
$model = $model->withCount($withCount);
|
|
}
|
|
if (isset($data['limit']) && $data['limit']) {
|
|
|
|
return $model->latest('id')->take($data['limit'])->get();
|
|
}
|
|
if (isset($data['page']) && $data['page']) {
|
|
|
|
return $model->paginate(15);
|
|
}
|
|
return $model->get();
|
|
}
|
|
|
|
public function findBy(string $key = '',string $value = '') {
|
|
if(empty($key) || empty($value))
|
|
{
|
|
return false;
|
|
}
|
|
return $this->model->where($key,$value)->first();
|
|
}
|
|
}
|