generated from liutk/owl-admin-base
64 lines
1.7 KiB
PHP
64 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Slowlyo\OwlAdmin\Controllers\AdminController as Controller;
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
|
use Throwable;
|
|
|
|
/**
|
|
* @property \App\Admin\Services\BaseService $service
|
|
*/
|
|
abstract class AdminController extends Controller
|
|
{
|
|
public function update(Request $request)
|
|
{
|
|
$input = $request->all();
|
|
|
|
if ($this->actionOfQuickEditItem()) {
|
|
Arr::pull($input, $this->service->primaryKey());
|
|
|
|
if ($request->filled('_editable')) {
|
|
$input = Arr::only($input, explode(',', $request->input('_editable')));
|
|
}
|
|
}
|
|
|
|
try {
|
|
$result = $this->service->update($this->getPrimaryValue($request), $input);
|
|
} catch (Throwable $th) {
|
|
return $this->renderExceptionResponse(
|
|
tap($th, fn () => report($th))
|
|
);
|
|
}
|
|
|
|
return $this->autoResponse($result, __('admin.save'));
|
|
}
|
|
|
|
public function getQuickEditItemPath(array $editable = ['*'])
|
|
{
|
|
$params = ['_action' => 'quickEditItem'];
|
|
|
|
if ($editable != ['*']) {
|
|
$params['_editable'] = implode(',', $editable);
|
|
}
|
|
|
|
return $this->getUpdatePath().'?'.http_build_query($params);
|
|
}
|
|
|
|
protected function renderExceptionResponse(Throwable $e)
|
|
{
|
|
$message = 'Server error';
|
|
|
|
if ($e instanceof ValidationException) {
|
|
$message = $e->validator->errors()->first();
|
|
} elseif ($e instanceof HttpExceptionInterface) {
|
|
$message = $e->getMessage();
|
|
}
|
|
|
|
return $this->response()->fail($message);
|
|
}
|
|
}
|