55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\{Article, ArticleCategory};
|
|
use App\Filters\ArticleFilter;
|
|
|
|
/**
|
|
* @method Article getModel()
|
|
* @method Article|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class ArticleService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['category'];
|
|
|
|
protected string $modelName = Article::class;
|
|
|
|
protected string $modelFilterName = ArticleFilter::class;
|
|
|
|
public function listQuery()
|
|
{
|
|
$model = $this->getModel();
|
|
$filter = $this->getModelFilter();
|
|
|
|
$query = $this->query();
|
|
if($this->withRelationships){
|
|
$query->with($this->withRelationships);
|
|
}
|
|
|
|
if ($filter) {
|
|
$query->filter(request()->input(), $filter);
|
|
}
|
|
|
|
return $query->sort();
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$cid = data_get($data, 'category_id');
|
|
if ($cid && $category = ArticleCategory::find($cid)) {
|
|
$data['category_path'] = $category->path . $category->id . '-';
|
|
}
|
|
return parent::update($primaryKey, $data);
|
|
}
|
|
|
|
public function store($data): bool
|
|
{
|
|
$cid = data_get($data, 'category_id');
|
|
if ($cid && $category = ArticleCategory::find($cid)) {
|
|
$data['category_path'] = $category->path . $category->id . '-';
|
|
}
|
|
return parent::store($data);
|
|
}
|
|
}
|