ldc-agis/app/Http/Controllers/Bo/TestimonialController.php
Noah Lancien bed9d9dde6 phpstan
2024-01-26 16:08:12 +01:00

167 lines
5.6 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\Testimonial;
use App\Traits\Controllers\ChangesModelOrder;
use App\Traits\Controllers\UpdateModelPublished;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class TestimonialController extends Controller
{
use UpdateModelPublished;
use ChangesModelOrder;
/**
* 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,
'description',
'job'
);
}
$searchFields = \implode(', ', [
Str::ucfirst(trans('validation.custom.description')),
Str::ucfirst(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 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
{
return view('bo.pages.testimonials.create', ['testimonialModel' => $testimonial]);
}
/**
* 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();
if ($testimonial->fill($request->validated())->saveOrFail()) {
return redirect()->route('bo.testimonials.edit', $testimonial)
->with('success', __('crud.messages.has_been_created', [
'model' => Str::ucfirst(__('models.classes.testimonial'))
]));
}
return redirect()->back()
->with('error', __('crud.messages.cannot_be_created', [
'model' => Str::ucfirst(__('models.classes.testimonial'))
]));
});
}
/**
* 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
{
return view('bo.pages.testimonials.edit', ['testimonialModel' => $testimonial]);
}
/**
* 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) {
if ($testimonial->fill($request->validated())->saveOrFail()) {
return redirect()->route('bo.testimonials.edit', $testimonial)
->with('success', __('crud.messages.has_been_updated', [
'model' => Str::ucfirst(__('models.classes.testimonial'))
]));
}
return redirect()->route('bo.testimonials.edit', $testimonial)
->with('error', __('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(__('models.classes.testimonial'))
]));
});
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Testimonial $testimonial
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Testimonial $testimonial): \Illuminate\Http\RedirectResponse
{
if ($testimonial->deleteOrFail()) {
return redirect()->route('bo.testimonials.index')
->with('success', __('crud.messages.has_been_deleted', [
'model' => Str::ucfirst(__('models.classes.testimonial'))
]));
}
return redirect()->back()
->with('error', __('crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst(__('models.classes.testimonial'))
]));
}
/**
* 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());
}
}