162 lines
5.1 KiB
PHP
162 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Stamps\StoreStampRequest;
|
|
use App\Http\Requests\Bo\Stamps\UpdateStampRequest;
|
|
use App\Models\Stamp;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StampController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Stamp::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 $stampModels */
|
|
$stampModels = Stamp::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$stampModels,
|
|
$search,
|
|
null,
|
|
'picture_alt',
|
|
'picture_title',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
Str::ucfirst(trans('validation.custom.attribute_alt')),
|
|
trans('validation.custom.attribute_title'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($stampModels);
|
|
|
|
/** Custom pagination */
|
|
$stampModels = $this->paginate($stampModels);
|
|
|
|
return view('bo.pages.stamps.index', compact('stampModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Stamp $stamp
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Stamp $stamp): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.stamps.create', ['stampModel' => $stamp]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Stamps\StoreStampRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreStampRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$stamp = new Stamp();
|
|
|
|
if ($stamp->fill($request->validated())->saveOrFail()) {
|
|
return redirect()->route('bo.stamps.edit', $stamp)
|
|
->with('success', __('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(__('models.classes.stamp'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(__('models.classes.stamp'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param App\Models\Stamp $stamp
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Stamp $stamp): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.stamps.edit', ['stampModel' => $stamp]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Stamps\UpdateStampRequest $request
|
|
* @param \App\Models\Stamp $stamp
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateStampRequest $request, Stamp $stamp): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $stamp) {
|
|
if ($stamp->fill($request->validated())->saveOrFail()) {
|
|
return redirect()->route('bo.stamps.edit', $stamp)
|
|
->with('success', __('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(__('models.classes.stamp'))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.stamps.edit', $stamp)
|
|
->with('error', __('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(__('models.classes.stamp'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Stamp $stamp
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Stamp $stamp): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if ($stamp->deleteOrFail()) {
|
|
return redirect()->route('bo.stamps.index')
|
|
->with('success', __('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(__('models.classes.stamp'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', __('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(__('models.classes.stamp'))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Stamp $stamp
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Stamp $stamp): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($stamp->replicate());
|
|
}
|
|
}
|