157 lines
5 KiB
PHP
157 lines
5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Labels\StoreLabelRequest;
|
|
use App\Http\Requests\Bo\Labels\UpdateLabelRequest;
|
|
use App\Models\Label;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class LabelController extends Controller
|
|
{
|
|
/**
|
|
* 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 $labelModels */
|
|
$labelModels = Label::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$labelModels,
|
|
$search,
|
|
null,
|
|
'name'
|
|
);
|
|
}
|
|
$searchFields = trans('Nom');
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($labelModels);
|
|
|
|
/** Custom pagination */
|
|
$labelModels = $this->customPaginate($labelModels, $request->pagination);
|
|
|
|
return view('bo.pages.labels.index', compact('labelModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Label $label
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Label $label): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.labels.create', [
|
|
'labelModel' => $label
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Labels\StoreLabelRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreLabelRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$label = new Label();
|
|
$label->fill($request->validated());
|
|
|
|
if ($label->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.labels.edit', $label)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(trans('models.classes.labels'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(trans('models.classes.labels'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Label $label
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Label $label): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.labels.edit', [
|
|
'labelModel' => $label,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Labels\UpdateLabelRequest $request
|
|
* @param \App\Models\Label $label
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
|
|
*/
|
|
public function update(UpdateLabelRequest $request, Label $label): \Illuminate\Http\RedirectResponse
|
|
{
|
|
// phpcs:enable
|
|
return DB::transaction(function () use ($request, $label) {
|
|
$label->fill($request->validated());
|
|
if ($label->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.labels.edit', $label)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => Str::singular(trans('models.classes.labels'))]
|
|
));
|
|
} //end if
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => Str::singular(trans('models.classes.labels'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Label $label
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Label $label): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($label) {
|
|
if ($label->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.labels.index')
|
|
->with('success', trans(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => Str::singular(trans('models.classes.labels'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => Str::singular(trans('models.classes.labels'))]
|
|
));
|
|
});
|
|
}
|
|
}
|