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

176 lines
5.5 KiB
PHP
Raw Permalink Normal View History

<?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;
2026-09-25 16:25:31 +02:00
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class TestimonialController extends Controller
{
use UpdateModelPublished;
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(Testimonial::class);
}
/**
* Display a listing of the resource.
*/
2026-09-25 16:25:31 +02:00
public function index(Request $request): 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::ucFirst(\implode(', ', [
trans_choice('validation.custom.catchline', 1),
trans('validation.attributes.content'),
trans('validation.custom.testimonial_identity'),
trans('validation.custom.job'),
]));
/** 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.
*/
2026-09-25 16:25:31 +02:00
public function show(Testimonial $testimonial): View
{
return view('bo.pages.testimonials.show', ['testimonialModel' => $testimonial]);
}
/**
* Show the form for creating a new resource.
*/
2026-09-25 16:25:31 +02:00
public function create(Testimonial $testimonial): View
{
$meatModels = Meat::query()->orderBy('order')->get();
return view('bo.pages.testimonials.create', [
'testimonialModel' => $testimonial,
'meatModels' => $meatModels,
]);
}
/**
* Store a newly created resource in storage.
*/
2026-09-25 16:25:31 +02:00
public function store(StoreTestimonialRequest $request): 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', trans('crud.messages.has_been_created', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucFirst(trans('models.classes.testimonial')),
]));
}
2026-09-25 16:25:31 +02:00
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_created', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucFirst(trans('models.classes.testimonial')),
]));
});
}
/**
* Show the form for editing the specified resource.
*/
2026-09-25 16:25:31 +02:00
public function edit(Testimonial $testimonial): View
{
$meatModels = Meat::query()->orderBy('order')->get();
return view('bo.pages.testimonials.edit', [
'testimonialModel' => $testimonial,
'meatModels' => $meatModels,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(
UpdateTestimonialRequest $request,
Testimonial $testimonial
2026-09-25 16:25:31 +02:00
): RedirectResponse {
return DB::transaction(function () use ($request, $testimonial) {
$testimonial->fill($request->validated());
if ($testimonial->saveOrFail()) {
return redirect()->route('bo.testimonials.edit', $testimonial)
->with('success', trans('crud.messages.has_been_updated', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucFirst(trans('models.classes.testimonial')),
]));
}
2026-09-25 16:25:31 +02:00
return redirect()->route('bo.testimonials.edit', $testimonial)
->with('error', trans('crud.messages.cannot_be_updated', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucFirst(trans('models.classes.testimonial')),
]));
});
}
/**
* Remove the specified resource from storage.
*/
2026-09-25 16:25:31 +02:00
public function destroy(Testimonial $testimonial): 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', trans('crud.messages.has_been_deleted', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucFirst(trans('models.classes.testimonial')),
]));
}
2026-09-25 16:25:31 +02:00
return redirect()->back()
->with('error', trans('crud.messages.cannot_be_deleted', [
2026-09-25 16:25:31 +02:00
'model' => Str::ucFirst(trans('models.classes.testimonial')),
]));
}
/**
* Duplicate the specified resource.
*/
2026-09-25 16:25:31 +02:00
public function duplicate(Testimonial $testimonial): View
{
return $this->create($testimonial->replicate());
}
}