175 lines
5.2 KiB
PHP
175 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\FaqQuestions\StoreFaqQuestionRequest;
|
|
use App\Http\Requests\Bo\FaqQuestions\UpdateFaqQuestionRequest;
|
|
use App\Models\FaqQuestion;
|
|
use App\Models\FaqTheme;
|
|
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 FaqQuestionController extends Controller
|
|
{
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(FaqQuestion::class);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
/** @var Builder $faqQuestionModels */
|
|
$faqQuestionModels = FaqQuestion::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$faqQuestionModels,
|
|
$search,
|
|
null,
|
|
'question',
|
|
'answer'
|
|
);
|
|
}
|
|
$searchFields = \sprintf(
|
|
'%s, %s ...',
|
|
Str::ucfirst(__('validation.custom.question')),
|
|
__('validation.custom.answer'),
|
|
);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($faqQuestionModels);
|
|
|
|
/** Custom pagination */
|
|
$faqQuestionModels = $this->paginate($faqQuestionModels);
|
|
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.faq-questions.index';
|
|
|
|
return view($view, [
|
|
'faqQuestionModels' => $faqQuestionModels,
|
|
'search' => $search,
|
|
'searchFields' => $searchFields,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create(FaqQuestion $faqQuestion): View
|
|
{
|
|
$faqThemeModels = FaqTheme::all();
|
|
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.faq-questions.create';
|
|
|
|
return view($view, [
|
|
'faqQuestionModel' => $faqQuestion,
|
|
'faqThemeModels' => $faqThemeModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreFaqQuestionRequest $request): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$faqQuestion = new FaqQuestion();
|
|
$faqQuestion->fill($request->validated());
|
|
if ($faqQuestion->saveOrFail()) {
|
|
return to_route('bo.faq-questions.edit', $faqQuestion)
|
|
->with(
|
|
'success',
|
|
trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => trans_choice('models.classes.faq-questions', 1)]
|
|
)
|
|
);
|
|
}
|
|
|
|
return back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => trans_choice('models.classes.faq-questions', 1)]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(FaqQuestion $faqQuestion): View
|
|
{
|
|
$faqThemeModels = FaqTheme::all();
|
|
|
|
/** @var view-string $view */
|
|
$view = 'back::pages.faq-questions.edit';
|
|
|
|
return view($view, [
|
|
'faqQuestionModel' => $faqQuestion,
|
|
'faqThemeModels' => $faqThemeModels,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(
|
|
UpdateFaqQuestionRequest $request,
|
|
FaqQuestion $faqQuestion
|
|
): RedirectResponse {
|
|
return DB::transaction(function () use ($request, $faqQuestion) {
|
|
$faqQuestion->fill($request->validated());
|
|
if ($faqQuestion->saveOrFail()) {
|
|
return to_route('bo.faq-questions.edit', $faqQuestion)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => trans_choice('models.classes.faq-questions', 1)]
|
|
));
|
|
}
|
|
|
|
return back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => trans_choice('models.classes.faq-questions', 1)]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(FaqQuestion $faqQuestion): RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($faqQuestion) {
|
|
if ($faqQuestion->deleteOrFail()) {
|
|
return to_route('bo.faq-questions.index')
|
|
->with('success', __(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => trans_choice('models.classes.faq-questions', 1)]
|
|
));
|
|
}
|
|
|
|
return back()
|
|
->with('error', __(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => trans_choice('models.classes.faq-questions', 1)]
|
|
));
|
|
});
|
|
}
|
|
}
|