1
0
Fork 0
owl-admin-starter/app/Services/UserService.php

62 lines
1.4 KiB
PHP

<?php
namespace App\Services;
use App\Exceptions\AdminValidationException;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Slowlyo\OwlAdmin\Services\AdminService;
/**
* 用户管理
*/
class UserService extends AdminService
{
protected string $modelName = User::class;
public function store($data)
{
$this->saving($data);
$model = $this->modelName::create($data);
$this->saved($model);
return true;
}
public function update($primaryKey, $data)
{
$this->saving($data, $primaryKey);
$model = $this->query()->whereKey($primaryKey)->first();
$model->update($data);
$this->saved($model, true);
return true;
}
public function saving(&$data, $primaryKey = '')
{
$unique = Rule::unique('users', 'phone');
$createRules = [
'phone' => ['required', $unique, 'phone']
];
$updateRules = [
'phone' => ['phone', $unique->ignore($primaryKey)]
];
$validator = Validator::make($data, $primaryKey ? $updateRules : $createRules);
if ($validator->fails()) {
throw new AdminValidationException($validator->errors());
}
}
public function sortable($query)
{
$query->orderByDesc('id');
}
public function searchable($query)
{
$query->filter(request()->all());
}
}