generated from panliang/owl-admin-starter
64 lines
1.5 KiB
PHP
64 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\ModelFilters\ArticleFilter;
|
|
use App\Models\Article;
|
|
use App\Models\Keyword;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
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 resloveData($data, $model = null)
|
|
{
|
|
$cid = data_get($data, 'category_id');
|
|
if ($cid && $category = Keyword::find($cid)) {
|
|
$data['category_path'] = $category->path.$category->id.'-';
|
|
}
|
|
if (! data_get($data, 'published_at')) {
|
|
$data['published_at'] = now();
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'title' => 'required',
|
|
];
|
|
$updateRules = [
|
|
];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules, [
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|