ldc-doux/app/Http/Controllers/GammesController.php
2022-07-27 14:33:56 +02:00

53 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Enums\Page;
use App\Models\Gamme;
use App\Models\Product;
use App\Models\SeoDatum;
class GammesController extends Controller
{
public function index()
{
$gammes = Gamme::whereNull('parent_gamme_id')->orderBy('title')->get();
//remove Food Service from count
/** @var \Illuminate\Database\Eloquent\Collection $gammes */
$gammes = $gammes->whereNotIn('slug', 'food-service');
$nb_gammes = $gammes->count();
$page = Page::gammes();
$seoDatum = SeoDatum::where('page', $page)->first();
return view('front.gammes.index', compact('gammes', 'nb_gammes', 'seoDatum'));
}
public function show(Gamme $gamme)
{
$gammes = collect([]);
$dbGammes = Gamme::where('id', $gamme->id)
->with('childGammes')->get();
/** @var \App\Models\Gamme $dbGamme */
foreach ($dbGammes as $dbGamme) {
$gammes->push($dbGamme->id);
$dbGamme->childGammes->sortBy('title')
->map(function (Gamme $childGamme) use (&$gammes) {
$gammes->push($childGamme->id);
});
}
$products = Product::whereIn('gamme_id', $gammes)
->where('is_published', 1)
->orderBy('name')
->get();
//make ghost seoDatum from the gamme
$seoDatum = new SeoDatum();
$seoDatum->title = $gamme->title;
$seoDatum->description = $gamme->text;
return view('front.gammes.show', compact('gamme', 'products', 'seoDatum'));
}
}