182 lines
6.2 KiB
PHP
182 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Professions\StoreProfessionRequest;
|
|
use App\Http\Requests\Bo\Professions\UpdateProfessionRequest;
|
|
use App\Models\Profession;
|
|
use App\Traits\Controllers\UpdateModelPublished;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ProfessionController extends Controller
|
|
{
|
|
use UpdateModelPublished;
|
|
|
|
/**
|
|
* Create the controller instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->authorizeResource(Profession::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 $professionModels */
|
|
$professionModels = Profession::query();
|
|
|
|
/** @var string $search Search field */
|
|
$search = $request->search;
|
|
if ($search) {
|
|
$this->searchQuery(
|
|
$professionModels,
|
|
$search,
|
|
null,
|
|
'first_name',
|
|
'last_name',
|
|
'email',
|
|
'role',
|
|
);
|
|
}
|
|
$searchFields = \implode(', ', [
|
|
trans('validation.attributes.first_name'),
|
|
trans('validation.attributes.last_name'),
|
|
trans('validation.attributes.email'),
|
|
trans('validation.attributes.role'),
|
|
]);
|
|
|
|
/** Sort columns with a query */
|
|
$this->sortQuery($professionModels);
|
|
|
|
/** Custom pagination */
|
|
$professionModels = $this->paginate($professionModels);
|
|
|
|
return view('bo.pages.professions.index', compact('professionModels', 'search', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*
|
|
* @param \App\Models\Profession $profession
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function show(Profession $profession): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.professions.show', ['professionModel' => $profession]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param \App\Models\Profession $profession
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Profession $profession): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.professions.create', ['professionModel' => $profession]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Professions\StoreProfessionRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreProfessionRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$profession = new Profession();
|
|
$profession->fill($request->validated());
|
|
|
|
if ($profession->saveOrFail()) {
|
|
return redirect()->route('bo.professions.edit', $profession)
|
|
->with('success', trans('crud.messages.has_been_created', [
|
|
'model' => Str::ucfirst(trans('models.classes.profession'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_created', [
|
|
'model' => Str::ucfirst(trans('models.classes.profession'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Profession $profession
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Profession $profession): \Illuminate\Contracts\View\View
|
|
{
|
|
return view('bo.pages.professions.edit', ['professionModel' => $profession]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Professions\UpdateProfessionRequest $request
|
|
* @param \App\Models\Profession $profession
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateProfessionRequest $request, Profession $profession): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $profession) {
|
|
$profession->fill($request->validated());
|
|
|
|
if ($profession->saveOrFail()) {
|
|
return redirect()->route('bo.professions.edit', $profession)
|
|
->with('success', trans('crud.messages.has_been_updated', [
|
|
'model' => Str::ucfirst(trans('models.classes.profession'))
|
|
]));
|
|
}
|
|
return redirect()->route('bo.professions.edit', $profession)
|
|
->with('error', trans('crud.messages.cannot_be_updated', [
|
|
'model' => Str::ucfirst(trans('models.classes.profession'))
|
|
]));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Profession $profession
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Profession $profession): \Illuminate\Http\RedirectResponse
|
|
{
|
|
/** @var array<string,string> */
|
|
$previousQueries = [];
|
|
\parse_str(\parse_url(url()->previous(), \PHP_URL_QUERY), $previousQueries);
|
|
if ($profession->deleteOrFail()) {
|
|
return redirect()->route('bo.professions.index', $previousQueries)
|
|
->with('success', trans('crud.messages.has_been_deleted', [
|
|
'model' => Str::ucfirst(trans('models.classes.profession'))
|
|
]));
|
|
}
|
|
return redirect()->back()
|
|
->with('error', trans('crud.messages.cannot_be_deleted', [
|
|
'model' => Str::ucfirst(trans('models.classes.profession'))
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Duplicate the specified resource.
|
|
*
|
|
* @param \App\Models\Profession $profession
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function duplicate(Profession $profession): \Illuminate\Contracts\View\View
|
|
{
|
|
return $this->create($profession->replicate());
|
|
}
|
|
}
|