generated from liutk/owl-admin-base
59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services\Train;
|
|
|
|
use App\Admin\Filters\TrianBookFilter;
|
|
use App\Models\Train\Book;
|
|
use App\Admin\Services\BaseService;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\{Validator, Storage};
|
|
|
|
class BookService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['category'];
|
|
|
|
protected string $modelName = Book::class;
|
|
|
|
protected string $modelFilterName = TrianBookFilter::class;
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if (isset($data['cover_image']) && $data['cover_image']) {
|
|
$image = $data['cover_image'];
|
|
$data['cover_image'] = Str::startsWith($image, ['http://', 'https://']) ? $image : Storage::url($image);
|
|
}
|
|
if (isset($data['video']) && $data['video']) {
|
|
$image = $data['video'];
|
|
$data['video'] = Str::startsWith($image, ['http://', 'https://']) ? $image : Storage::url($image);
|
|
}
|
|
if (isset($data['files']) && $data['files']) {
|
|
$files = [];
|
|
foreach ($data['files'] as $value) {
|
|
$path = data_get($value, 'value');
|
|
$value['url'] = Str::startsWith($path, ['http://', 'https://']) ? $path : Storage::url($path);
|
|
array_push($files, $value);
|
|
}
|
|
$data['files'] = $files;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'title' => ['required'],
|
|
'catgeory_id' => ['required'],
|
|
'type' => ['required'],
|
|
'files' => ['array'],
|
|
];
|
|
$updateRules = [
|
|
'files' => ['array'],
|
|
];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
return true;
|
|
}
|
|
}
|