55 lines
1.3 KiB
PHP
55 lines
1.3 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 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());
|
|
}
|
|
|
|
public function deleted($ids)
|
|
{
|
|
$ids = explode(',', $ids);
|
|
|
|
$this->query()->where(function ($q) use ($ids) {
|
|
foreach ($ids as $id) {
|
|
$q->orWhere('path', 'like', '%-'.$id.'-%');
|
|
}
|
|
})->delete();
|
|
}
|
|
} |