ldc-legaulois/app/Http/Controllers/Bo/TestimonialController.php

195 lines
6.5 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Testimonials\StoreTestimonialRequest;
use App\Http\Requests\Bo\Testimonials\UpdateTestimonialRequest;
use App\Models\Meat;
use App\Models\Testimonial;
use App\Traits\Controllers\UpdateModelPublished;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TestimonialController extends Controller
{
use UpdateModelPublished;
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(Testimonial::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 $testimonialModels */
$testimonialModels = Testimonial::query();
/** @var string $search Search field */
$search = $request->search;
if ($search) {
$this->searchQuery(
$testimonialModels,
$search,
null,
'catchline',
'content',
'identity',
'job',
);
}
$searchFields = str(\implode(', ', [
trans_choice('validation.custom.catchline', 1),
trans('validation.attributes.content'),
trans('validation.custom.testimonial_identity'),
trans('validation.custom.job'),
]))->ucfirst();
/** Sort columns with a query */
$this->sortQuery($testimonialModels);
/** Custom pagination */
$testimonialModels = $this->paginate($testimonialModels);
return view('bo.pages.testimonials.index', compact('testimonialModels', 'search', 'searchFields'));
}
/**
* Show a specific resource.
*
* @param \App\Models\Testimonial $testimonial
* @return \Illuminate\Contracts\View\View
*/
public function show(Testimonial $testimonial): \Illuminate\Contracts\View\View
{
return view('bo.pages.testimonials.show', ['testimonialModel' => $testimonial]);
}
/**
* Show the form for creating a new resource.
*
* @param \App\Models\Testimonial $testimonial
* @return \Illuminate\Contracts\View\View
*/
public function create(Testimonial $testimonial): \Illuminate\Contracts\View\View
{
$meatModels = Meat::query()->orderBy('order')->get();
return view('bo.pages.testimonials.create', [
'testimonialModel' => $testimonial,
'meatModels' => $meatModels,
]);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\Bo\Testimonials\StoreTestimonialRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreTestimonialRequest $request): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($request) {
$testimonial = new Testimonial();
$testimonial->fill($request->validated());
if ($testimonial->saveOrFail()) {
return redirect()->route('bo.testimonials.edit', $testimonial)
->with('success', __('crud.messages.has_been_created', [
'model' => str(trans('models.classes.testimonial'))->ucfirst()
]));
}
return redirect()->back()
->with('error', __('crud.messages.cannot_be_created', [
'model' => str(trans('models.classes.testimonial'))->ucfirst()
]));
});
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Testimonial $testimonial
* @return \Illuminate\Contracts\View\View
*/
public function edit(Testimonial $testimonial): \Illuminate\Contracts\View\View
{
$meatModels = Meat::query()->orderBy('order')->get();
return view('bo.pages.testimonials.edit', [
'testimonialModel' => $testimonial,
'meatModels' => $meatModels,
]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\Bo\Testimonials\UpdateTestimonialRequest $request
* @param \App\Models\Testimonial $testimonial
* @return \Illuminate\Http\RedirectResponse
*/
public function update(
UpdateTestimonialRequest $request,
Testimonial $testimonial
): \Illuminate\Http\RedirectResponse {
return DB::transaction(function () use ($request, $testimonial) {
$testimonial->fill($request->validated());
if ($testimonial->saveOrFail()) {
return redirect()->route('bo.testimonials.edit', $testimonial)
->with('success', __('crud.messages.has_been_updated', [
'model' => str(trans('models.classes.testimonial'))->ucfirst()
]));
}
return redirect()->route('bo.testimonials.edit', $testimonial)
->with('error', __('crud.messages.cannot_be_updated', [
'model' => str(trans('models.classes.testimonial'))->ucfirst()
]));
});
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Testimonial $testimonial
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Testimonial $testimonial): \Illuminate\Http\RedirectResponse
{
/** @var array<string,string> */
$previousQueries = [];
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
if ($testimonial->deleteOrFail()) {
return redirect()->route('bo.testimonials.index', $previousQueries)
->with('success', __('crud.messages.has_been_deleted', [
'model' => str(trans('models.classes.testimonial'))->ucfirst()
]));
}
return redirect()->back()
->with('error', __('crud.messages.cannot_be_deleted', [
'model' => str(trans('models.classes.testimonial'))->ucfirst()
]));
}
/**
* Duplicate the specified resource.
*
* @param \App\Models\Testimonial $testimonial
* @return \Illuminate\Contracts\View\View
*/
public function duplicate(Testimonial $testimonial): \Illuminate\Contracts\View\View
{
return $this->create($testimonial->replicate());
}
}