ldc-domaine-de-lanvaux/app/Http/Controllers/GammesController.php
2023-06-05 15:09:57 +02:00

55 lines
1.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Gamme;
use App\Models\Product;
use App\Models\BasicBanner;
use App\Models\GammeHook;
class GammesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$gammes = Gamme::whereNull('parent_gamme_id')->orderBy('order')->get();
$gammebanner = BasicBanner::where('slug', 'main_gammebanner')->first();
$gammehook = GammeHook::where('slug', 'gamme_hook')->first();
$sousGammes = Gamme::whereNotNull('parent_gamme_id')->orderBy('parent_gamme_id')->get();
return view('front.gammes.index', compact('gammes', 'gammebanner', 'gammehook', 'sousGammes'));
}
/**
* Show a specific gamme.
*
* @param Gamme $gamme
* @return \Illuminate\Contracts\View\View
*/
public function show(Gamme $gamme): \Illuminate\Contracts\View\View
{
$gammes = collect([]);
$dbGammes = Gamme::where('id', $gamme->id)
->with('childGammes')->get();
$parent_gamme = Gamme::whereNull('parent_gamme_id')->orderBy('order')->get();
/** @var \App\Models\Gamme $dbGamme */
foreach ($dbGammes as $dbGamme) {
$gammes->push($dbGamme->id);
$dbGamme->childGammes->sortBy('order')
->map(function (Gamme $childGamme) use (&$gammes) {
$gammes->push($childGamme->id);
});
}
$products = Product::whereIn('gamme_id', $gammes)
->orderBy('order')
->get();
return view('front.gammes.show', compact('gamme', 'products', 'parent_gamme'));
}
}