165 lines
5.4 KiB
PHP
165 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\Recipes\StoreRecipeRequest;
|
|
use App\Http\Requests\Bo\Recipes\UpdateRecipeRequest;
|
|
use App\Models\Range;
|
|
use App\Models\Recipe;
|
|
use App\Traits\Controllers\UpdateModelStatus;
|
|
use App\Traits\Controllers\ChangesModelOrder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RecipeController extends Controller
|
|
{
|
|
use ChangesModelOrder;
|
|
use UpdateModelStatus;
|
|
|
|
/**
|
|
* 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 $recipe */
|
|
$recipe = Recipe::query();
|
|
|
|
if ($request->search) {
|
|
$this->searchQuery(
|
|
$recipe,
|
|
$request->search,
|
|
null,
|
|
'name',
|
|
);
|
|
}
|
|
|
|
$this->sortQuery($recipe);
|
|
$searchFields = trans('Nom');
|
|
|
|
$recipeModels = $recipe->paginate(10);
|
|
|
|
return view('bo.pages.recipes.index', compact('recipeModels', 'searchFields'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(): \Illuminate\Contracts\View\View
|
|
{
|
|
$rangeModels = Range::orderBy('order', 'ASC')->get();
|
|
|
|
return view('bo.pages.recipes.create', compact('rangeModels'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Recipes\StoreRecipeRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreRecipeRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$recipe = new Recipe();
|
|
$tmpIngredients = \collect($request->validated()['ingredients'])->sortKeys()->toArray();
|
|
$recipe->fill($request->validated());
|
|
$recipe->ingredients = $tmpIngredients;
|
|
|
|
if ($recipe->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.recipes.edit', $recipe)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_created',
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_created',
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Recipe $recipe): \Illuminate\Contracts\View\View
|
|
{
|
|
$rangeModels = Range::orderBy('order', 'ASC')->get();
|
|
|
|
return view('bo.pages.recipes.edit', [
|
|
'recipeModel' => $recipe,
|
|
'rangeModels' => $rangeModels
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \App\Http\Requests\Bo\Recipes\UpdateRecipeRequest $request
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateRecipeRequest $request, Recipe $recipe): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $recipe) {
|
|
$tmpIngredients = \collect($request->validated()['ingredients'])->sortKeys()->toArray();
|
|
$recipe->fill($request->validated());
|
|
$recipe->ingredients = $tmpIngredients;
|
|
|
|
if ($recipe->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.recipes.edit', $recipe)
|
|
->with('success', trans(
|
|
'crud.messages.has_been_updated',
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
));
|
|
} //end if
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
'crud.messages.cannot_be_updated',
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\Recipe $recipe
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Recipe $recipe): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($recipe) {
|
|
$recipe->products()->detach();
|
|
if ($recipe->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.recipes.index')
|
|
->with('success', trans(
|
|
'crud.messages.has_been_deleted',
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
));
|
|
}
|
|
return redirect()
|
|
->back()
|
|
->with('error', trans(
|
|
':crud.messages.cannot_be_deleted',
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
));
|
|
});
|
|
}
|
|
}
|