@royal sluice and @oblique talon:
First, I'm very appreciative you took the time to look at it and provide feedback. Let me first say, I am not trying to defend the code, but only explaining why I did what I did as to maybe allow you to give me better constructive feedback. Here was my thought process:
The end result is I wanted to basically show App\Models\Project in different 'Contexts'. So for instance if you are looking at "All" projects, then it may just be a bunch of thumbnails with titles and the date next to it. But if you filter it to just art, it would grab all Project::where('type', 'art') and show it in a different context - for instance the large version of the image whereas type => strategy would show a lot more text and maybe the problem I was trying to solve.
Since, the rendering logic would need to change based on what you were looking at I decided to create the "Section" object which holds the logic of how to render the section in the portfolio. And the first thing the Controller does is just pass a Request to the Section for the Section to figure out which type of Section to return "All, Software, Image, Strategy, etc."
$portfolio = Section::create($request);
abstract class Section
{
private string $title;
private string $slug;
private Collection $projects;
const allSlug = 'all';
static private array $sections = [
self::allSlug => All::class,
Software::slug => Software::class,
Art::slug => Art::class,
Strategy::slug => Strategy::class,
];
static public function create(Request $request) : Section
{
if($request->route()->getName() === PortfolioController::route) {
$filter = $request->route()->parameter('filter') ?: self::allSlug;
return new self::$sections[$filter]($request);
}
throw new \Exception('Invalid route for portfolio section');
}
}
class All extends Section
{
public function getTitle(): string
{
return 'All My Work';
}
public function getSubTitle(): string
{
return 'A collection of all my work over 20 years';
}
public function renderProject(Project $project) : string
{
return view('components.portfolio.project.all', ['project' => $project]);
}
}
class Art extends Section
{
const slug = 'art';
public function getTitle(): string
{
return 'Digital Art';
}
public function getSubTitle(): string
{
return 'A collection of my digital art infused with AI';
}
public function renderProject(Project $project): string
{
return view('components.portfolio.project.art', ['project' => $project]);
}
}
<x-portfolio.projects :section="$portfolio" />
<x-site.section :title="$title" :sub-title="$section->getSubTitle()">
@foreach($projects as $project)
{!! $section->renderProject($project) !!}
@endforeach
</x-site.section>
class Projects extends Component
{
public Section $section;
public string $title;
public Collection $projects;
public function __construct(Section $section)
{
$this->section = $section;
$this->title = $section->getTitle();
$this->subTitle = $section->getSubTitle();
}
}