Creating a front-end for parent-child nested module

Hello, i need help with the frontend controller for a Parent-child modules: i want to create articles in categories, i have successfully created Category and CategoryArticle classes like in the documentation but i cannot make the page routing work web.php
Route::get('/categories/{slug}', [CategoryArticleDisplayController::class, 'show'])->name('frontend.category')->where('slug', '.*');
Route::get('/categories/{slug}', [CategoryArticleDisplayController::class, 'show'])->name('frontend.category')->where('slug', '.*');
CategoryArticleDisplayController.php
<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\CategoryArticle;
use App\Repositories\CategoryRepository;
use App\Repositories\CategoryArticleRepository;
use Illuminate\Http\Request;

class CategoryArticleDisplayController extends Controller
{
public function show(string $slug, CategoryRepository $categoryRepository, CategoryArticleRepository $articleRepository): View
{

$category = $categoryRepository->forSlug($slug);
if ($category) {
return $this->showCategory($category);////<<<THIS WORKS
}

$article = $articleRepository->forSlug($slug);///THIS CANNOT FIND THE ARTICLE
if ($article) {

return $this->showArticle($article);
}

abort(404);
}

protected function showCategory(Category $category): View
{
dd('category', $category);

return view('site.category', ['item' => $category]);
}


protected function showArticle(CategoryArticle $article): View
{

dd('article', $article);
return view('site.category-article', ['item' => $article]);
}
}
<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\CategoryArticle;
use App\Repositories\CategoryRepository;
use App\Repositories\CategoryArticleRepository;
use Illuminate\Http\Request;

class CategoryArticleDisplayController extends Controller
{
public function show(string $slug, CategoryRepository $categoryRepository, CategoryArticleRepository $articleRepository): View
{

$category = $categoryRepository->forSlug($slug);
if ($category) {
return $this->showCategory($category);////<<<THIS WORKS
}

$article = $articleRepository->forSlug($slug);///THIS CANNOT FIND THE ARTICLE
if ($article) {

return $this->showArticle($article);
}

abort(404);
}

protected function showCategory(Category $category): View
{
dd('category', $category);

return view('site.category', ['item' => $category]);
}


protected function showArticle(CategoryArticle $article): View
{

dd('article', $article);
return view('site.category-article', ['item' => $article]);
}
}
1 Reply
Tom M
Tom M8mo ago
There is no reason why this wouldn't work. I have just tried using the same code and it works fine for me. Are you sure you have a article with the slug that you're trying to find? And can you confirm that you're trying to access the article as: http://localhost/categories/{articleSlug}
Want results from more Discord servers?
Add your server