159 lines
5.2 KiB
PHP
159 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\CodesEan\StoreCodeEanRequest;
|
|
use App\Http\Requests\Bo\CodesEan\UpdateCodeEanRequest;
|
|
use App\Models\CodeEan;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class CodesEANController 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 $codeEanModels */
|
|
$codeEanModels = CodeEan::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$codeEanModels,
|
|
$search,
|
|
null,
|
|
'code',
|
|
'product'
|
|
);
|
|
}
|
|
$searchFields = \sprintf(
|
|
'%s, %s',
|
|
Str::ucfirst(__('validation.attributes.code')),
|
|
Str::ucfirst(__('validation.attributes.product'))
|
|
);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($codeEanModels);
|
|
|
|
/** Custom pagination */
|
|
$codeEanModels = $this->customPaginate($codeEanModels, $request->pagination);
|
|
|
|
return view('bo.pages.codes-ean.index', compact('codeEanModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\CodeEan $codeEan
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(CodeEan $codeEan): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.codes-ean.create', [
|
|
'codeEanModel' => $codeEan,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\CodesEan\StoreCodeEanRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreCodeEanRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$codeEan = new CodeEan();
|
|
$codeEan->fill($request->validated());
|
|
if ($codeEan->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.code-eans.edit', $codeEan)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => \trans_choice('models.classes.codes-ean', 1)]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => \trans_choice('models.classes.codes-ean', 1)]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\CodeEan $codeEan
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(CodeEan $codeEan): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.codes-ean.edit', [
|
|
'codeEanModel' => $codeEan,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\CodesEan\UpdateCodeEanRequest $request
|
|
* @param \App\Models\CodeEan $codeEan
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateCodeEanRequest $request, CodeEan $codeEan): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $codeEan) {
|
|
$codeEan->fill($request->validated());
|
|
if ($codeEan->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.code-eans.edit', $codeEan)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => trans_choice('models.classes.codes-ean', 1)]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => trans_choice('models.classes.codes-ean', 1)]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\CodeEan $codeEan
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(CodeEan $codeEan): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($codeEan) {
|
|
if ($codeEan->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.code-eans.index')
|
|
->with('success', __(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => trans_choice('models.classes.codes-ean', 1)]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', __(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => trans_choice('models.classes.codes-ean', 1)]
|
|
));
|
|
});
|
|
}
|
|
}
|