ldc-doux/app/Http/Controllers/GammesController.php
2022-08-29 17:23:10 +02:00

65 lines
1.8 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
{
/**
* Show the Gammes view.
*
* @return \Illuminate\Contracts\View\View
*/
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'));
}
/**
* Show one Gamme
*
* @param Gamme $gamme
*
* @return \Illuminate\Contracts\View\View
*/
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'));
}
}