2023-02-24 15:20:58 +00:00
|
|
|
<?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\Product;
|
|
|
|
|
use App\Models\Recipe;
|
|
|
|
|
use App\Traits\Controllers\UpdateModelStatus;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class RecipeController extends Controller
|
|
|
|
|
{
|
|
|
|
|
use UpdateModelStatus;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2024-09-06 08:30:55 +00:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
2023-02-24 15:20:58 +00:00
|
|
|
*/
|
2024-09-06 08:30:55 +00:00
|
|
|
public function index(Request $request): \Illuminate\Contracts\View\View
|
2023-02-24 15:20:58 +00:00
|
|
|
{
|
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder $recipes */
|
|
|
|
|
$recipes = Recipe::query();
|
|
|
|
|
|
|
|
|
|
if ($request->search) {
|
|
|
|
|
$this->searchQuery(
|
|
|
|
|
$recipes,
|
|
|
|
|
$request->search,
|
|
|
|
|
null,
|
|
|
|
|
'name',
|
|
|
|
|
'description',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->sortQuery($recipes);
|
|
|
|
|
$searchFields = trans('Nom');
|
|
|
|
|
|
|
|
|
|
$recipes = $recipes->paginate(10);
|
|
|
|
|
|
|
|
|
|
return view('bo.pages.recipes.index', compact('recipes', 'searchFields'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
2024-09-06 08:30:55 +00:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
2023-02-24 15:20:58 +00:00
|
|
|
*/
|
2024-09-06 08:30:55 +00:00
|
|
|
public function create(): \Illuminate\Contracts\View\View
|
2023-02-24 15:20:58 +00:00
|
|
|
{
|
|
|
|
|
$products = Product::all();
|
|
|
|
|
$recipes = Recipe::all();
|
|
|
|
|
|
|
|
|
|
return view('bo.pages.recipes.create', [
|
|
|
|
|
'products' => $products,
|
|
|
|
|
'recipes' => $recipes,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Bo\Recipes\StoreRecipeRequest $request
|
2024-09-06 08:30:55 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2023-02-24 15:20:58 +00:00
|
|
|
*/
|
2024-09-06 08:30:55 +00:00
|
|
|
public function store(StoreRecipeRequest $request): \Illuminate\Http\RedirectResponse
|
2023-02-24 15:20:58 +00:00
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request) {
|
|
|
|
|
$recipe = new Recipe();
|
|
|
|
|
$recipe->fill($request->validated());
|
|
|
|
|
|
|
|
|
|
if ($recipe->saveOrFail()) {
|
|
|
|
|
$recipe->products()->sync($request->product_list);
|
|
|
|
|
$recipe->recipes()->sync($request->recipe_list);
|
|
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('bo.recipes.edit', $recipe)
|
|
|
|
|
->with('success', trans(
|
|
|
|
|
':model a été créé',
|
|
|
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
|
|
|
|
':model n\'a pas été créé',
|
|
|
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Recipe $recipe
|
2024-09-06 08:30:55 +00:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
2023-02-24 15:20:58 +00:00
|
|
|
*/
|
2024-09-06 08:30:55 +00:00
|
|
|
public function edit(Recipe $recipe): \Illuminate\Contracts\View\View
|
2023-02-24 15:20:58 +00:00
|
|
|
{
|
|
|
|
|
$products = Product::all();
|
2024-09-06 08:30:55 +00:00
|
|
|
$recipes = Recipe::query()->where('id', '!=', $recipe->id)->get();
|
2023-02-24 15:20:58 +00:00
|
|
|
|
|
|
|
|
return view('bo.pages.recipes.edit', [
|
|
|
|
|
'recipeModel' => $recipe,
|
|
|
|
|
'products' => $products,
|
|
|
|
|
'recipes' => $recipes,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Bo\Recipes\UpdateRecipeRequest $request
|
|
|
|
|
* @param \App\Models\Recipe $recipe
|
2024-09-06 08:30:55 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2023-02-24 15:20:58 +00:00
|
|
|
*/
|
2024-09-06 08:30:55 +00:00
|
|
|
public function update(UpdateRecipeRequest $request, Recipe $recipe): \Illuminate\Http\RedirectResponse
|
2023-02-24 15:20:58 +00:00
|
|
|
{
|
|
|
|
|
return DB::transaction(function () use ($request, $recipe) {
|
|
|
|
|
$recipe->products()->sync($request->product_list);
|
|
|
|
|
$recipe->recipes()->sync($request->recipe_list);
|
|
|
|
|
|
|
|
|
|
$recipe->fill($request->validated());
|
|
|
|
|
|
|
|
|
|
if ($recipe->saveOrFail()) {
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('bo.recipes.edit', $recipe)
|
|
|
|
|
->with('success', trans(
|
|
|
|
|
':model a été mis à jour',
|
|
|
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
|
|
|
|
':model n\'a pas été mis à jour',
|
|
|
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Recipe $recipe
|
2024-09-06 08:30:55 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2023-02-24 15:20:58 +00:00
|
|
|
*/
|
2024-09-06 08:30:55 +00:00
|
|
|
public function destroy(Recipe $recipe): \Illuminate\Http\RedirectResponse
|
2023-02-24 15:20:58 +00:00
|
|
|
{
|
|
|
|
|
DB::table('recipe_recipe')->where($recipe->recipes()->getRelatedPivotKeyName(), $recipe->id)->delete();
|
|
|
|
|
$recipe->products()->sync([]);
|
|
|
|
|
$recipe->recipes()->sync([]);
|
|
|
|
|
|
|
|
|
|
if ($recipe->deleteOrFail()) {
|
|
|
|
|
return redirect()
|
|
|
|
|
->route('bo.recipes.index')
|
|
|
|
|
->with('warning', trans(
|
|
|
|
|
':model a bien été supprimé',
|
|
|
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
|
->back()
|
|
|
|
|
->with('error', trans(
|
|
|
|
|
':model n\'a pas été supprimé',
|
|
|
|
|
['model' => Str::singular(trans('models.classes.recipes'))]
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|