96 lines
2.8 KiB
PHP
96 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\StaticPages\UpdateStaticPageRequest;
|
|
use App\Models\StaticPage;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StaticPageController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(StaticPage::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
/** @var Builder $query */
|
|
$query = StaticPage::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$query,
|
|
$search,
|
|
null,
|
|
'title',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.title'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($query);
|
|
|
|
/** Custom pagination */
|
|
$staticPageModelList = $this->paginate($query);
|
|
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.static_pages.index';
|
|
|
|
return view($view, [
|
|
'staticPageModelList' => $staticPageModelList,
|
|
'search' => $search,
|
|
'searchFields' => $searchFields,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(StaticPage $static_page): View
|
|
{
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.static_pages.edit';
|
|
return view($view, ['staticPageModel' => $static_page]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(UpdateStaticPageRequest $request, StaticPage $static_page): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $static_page) {
|
|
$static_page->fill($request->validated());
|
|
|
|
if ($static_page->saveOrFail()) {
|
|
return to_route('bo.static_pages.edit', $static_page)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.static_page', 1)),
|
|
]));
|
|
}
|
|
|
|
return to_route('bo.static_pages.edit', $static_page)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(\trans_choice('models.classes.static_page', 1)),
|
|
]));
|
|
});
|
|
}
|
|
}
|