ldc-agis/app/Http/Controllers/Bo/ExplorerpageController.php
2023-10-24 10:08:32 +02:00

101 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Explorerpage\UpdateExplorerpageRequest;
use App\Models\Commitment;
use App\Models\Explorerpage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class ExplorerpageController extends Controller
{
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(Explorerpage::class);
}
/**
* Display a listing of the resource.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function index(Request $request): \Illuminate\Contracts\View\View
{
/** @var \Illuminate\Database\Eloquent\Builder $explorerpageModels */
$explorerpageModels = Explorerpage::query();
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$explorerpageModels,
$search,
null,
'picture_1_title',
'picture_2_title',
'picture_3_title'
);
}
$searchFields = \implode(', ', [
Str::ucfirst(trans('validation.attributes.title'))
]);
/** Sort columns with a query */
$this->sortQuery($explorerpageModels);
/** Custom pagination */
$explorerpageModels = $this->paginate($explorerpageModels);
return view('bo.pages.explorerpages.index', compact('explorerpageModels', 'search', 'searchFields'));
}
/**
* Show the form for editing the specified resource.
*
* @param App\Models\Explorerpage $explorerpage
* @return \Illuminate\Contracts\View\View
*/
public function edit(Explorerpage $explorerpage): \Illuminate\Contracts\View\View
{
$commitmentModels = Commitment::all();
return view('bo.pages.explorerpages.edit', [
'explorerpageModel' => $explorerpage,
'commitmentModels' => $commitmentModels,
]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\Bo\Explorerpages\UpdateExplorerpageRequest $request
* @param \App\Models\Explorerpage $explorerpage
* @return \Illuminate\Http\RedirectResponse
*/
public function update(
UpdateExplorerpageRequest $request,
Explorerpage $explorerpage
): \Illuminate\Http\RedirectResponse {
return DB::transaction(function () use ($request, $explorerpage) {
$explorerpage->fill($request->safe()->except('commitments'));
$explorerpage->commitments()->sync(\collect($request->validated('commitments'))->pluck('id'));
if ($explorerpage->saveOrFail()) {
return redirect()->route('bo.explorerpages.edit', $explorerpage)
->with('success', __('crud.messages.has_been_updated', [
'model' => Str::ucfirst(__('models.classes.explorerpage'))
]));
}
return redirect()->route('bo.explorerpages.edit', $explorerpage)
->with('error', __('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(__('models.classes.explorerpage'))
]));
});
}
}