Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

undefined $currentCategory?

@SilenceBringer and @tyKus sir i have another problem , I have done wit eager loading.. when i back to the / url it show me undefined error and when i am on this rul /categories/category:slug everything is fine. And when i put the header components code in the posts blade file nothing error occurs why ? Its showing me this error: Undefined variable $currentCategory my browser error ://Showing this line error in the browser

<?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'components.header-card','data' => ['categories' => $categories,'currentCategory' => $currentCategory]] + (isset($attributes) ? (array) $attributes- 

my routes code..


Route::get('/', function (Category $category) {
    $posts = Post::latest()->get();
    $categories = Category::all();
    return view('posts', compact('posts', 'categories'));
});

Route::get('/posts/{post:slug}', function (Post $post) {   
    return view('post' , [
        'post' => $post
    ]);
});

Route::get('/categories/{category:slug}', function (Category $category) {  
    return view('posts' , [
        'posts' => $category->posts,
        'currentCategory' => $category,
        'categories' => Category::all()
        
    ]);
});

Route::get('/authors/{author:username}', function (User $author) {  
    return view('posts' , [
        'posts' => $author->posts,
        'categories' => Category::all()
    ]);
});

my posts blade code

@extends('layouts.layout')
@section('content')
    <x-header-card :categories="$categories" :currentCategory="$currentCategory" />


    <main class="max-w-6xl mx-auto mt-6 lg:mt-20 space-y-6">
        @if($posts->count())
        <x-postsgrid :posts="$posts" />
        @else
        <p class="text-lg text-center ">
            You have no posts yet!.Please Check back later.
        </p>
        @endif

        <div class="lg:grid lg:grid-cols-3">

        </div>
    </main>
@endsection

my header component code

@props(['categories','currentCategory'])
<header class="max-w-xl mx-auto mt-20 text-center">
    <h1 class="text-4xl">
        Latest <span class="text-blue-500">Laravel From Scratch</span> News
    </h1>

    <h2 class="inline-flex mt-2">
        By Lary Laracore
        <img src="{{ url('images/lary-head.svg') }}" alt="Head of Lary the mascot">
    </h2>

    <p class="text-sm mt-14">
        Another year. Another update. We're refreshing the popular Laravel series with new content.
        I'm going to keep you guys up to speed with what's going on!
    </p>

    <div class="space-y-2 lg:space-y-0 lg:space-x-4 mt-8">
        <!--  Category -->
        <div class="relative flex lg:inline-flex items-center bg-gray-100 rounded-xl">
            <div x-data="{show:false}" class="flex w-full">
                <button @click="show = !show " 
                    class="py-2 pl-3 pr-9 text-sm font-semibold lg:w-32 inline-flex sm:w-full" @click.away="show=false">
                    {{ isset($currentCategory) ? ucwords($currentCategory->name) : 'Categories' }} 
                    {{-- {{ $currentCategory ? ucwords($currentCategory->name) : 'Categories' }} --}}
                    <svg class="transform -rotate-90 absolute pointer-events-none" style="right: 12px;" width="22"
                        height="22" viewBox="0 0 22 22">
                        <g fill="none" fill-rule="evenodd">
                            <path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
                            </path>
                            <path fill="#222"
                                d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z"></path>
                        </g>
                    </svg>
                </button>
                <div x-show="show" class="absolute bg-gray-100 w-full mt-10 rounded-xl z-50 ">
                    <a href="/"
                        class="
                            block w-full text-left px-3 text-sm leading-6 hover:bg-blue-600
                            hover:text-white  focus:bg-blue-600 focus:text-white"                         
                    >
                        All
                    </a>
                    {{-- @if($categories) --}}
                        @foreach ($categories as $category )
                        <a href="/categories/{{ $category->slug }}"
                            class="
                                block w-full text-left px-3 text-sm leading-6 hover:bg-blue-600
                                hover:text-white  focus:bg-blue-600 focus:text-white
                                {{ isset($currentCategory) && $currentCategory->id === $category->id ? 'bg-blue-600 text-white' : ''}}
                                "                         
                        >
                            {{ ucwords($category->name) }}
                        </a>
                    @endforeach
                    {{-- @endif --}}

                    

                </div>
            </div>
        </div>

        <!-- Other Filters -->
        <div class="relative flex lg:inline-flex items-center bg-gray-100 rounded-xl">
            <select class="flex-1 appearance-none bg-transparent py-2 pl-3 pr-9 text-sm font-semibold">
                <option value="category" disabled selected>Other Filters
                </option>
                <option value="foo">Foo
                </option>
                <option value="bar">Bar
                </option>
            </select>

            <svg class="transform -rotate-90 absolute pointer-events-none" style="right: 12px;" width="22" height="22"
                viewBox="0 0 22 22">
                <g fill="none" fill-rule="evenodd">
                    <path stroke="#000" stroke-opacity=".012" stroke-width=".5" d="M21 1v20.16H.84V1z">
                    </path>
                    <path fill="#222" d="M13.854 7.224l-3.847 3.856 3.847 3.856-1.184 1.184-5.04-5.04 5.04-5.04z">
                    </path>
                </g>
            </svg>
        </div>

        <!-- Search -->
        <div class="relative flex lg:inline-flex items-center bg-gray-100 rounded-xl px-3 py-2">
            <form method="GET" action="#">
                <input type="text" name="search" placeholder="Find something"
                    class="bg-transparent placeholder-black font-semibold text-sm">
            </form>
        </div>
    </div>
</header>
0 likes
4 replies
Sinnbeck's avatar

You are not passing $currentCategory here?

 return view('posts' , [
        'posts' => $author->posts,
        'categories' => Category::all()
    ]);
Shivamyadav's avatar

@Sinnbeck how can i pass $currentCategory thats a problem i am facing .. when i try it like this but also facing same issue

Route::get('/authors/{author:username}', function (User $author, Category $category) {  
    return view('posts' , [
        'posts' => $author->posts,
        'categories' => Category::all(),
        'currentCategory' => $category,
    ]);
});
Shivamyadav's avatar

@Sinnbeck but when i try to pass $currentCategory to here show the result from here https://snipboard.io/hjpW8K.jpg and not facing error but not getting the Categories value here where i have marked in the image dropdown . here is the header thats dropdown code..

{{ isset($currentCategory) ? ucwords($currentCategory->name) : 'Categories' }} 

in this route i get the result but not properly

Route::get('/', function (Category $category) {
    $posts = Post::latest()->get();
    $categories = Category::all();
    $currentCategory = $category;

    return view('posts', compact('posts', 'categories', 'currentCategory'));
});
SilenceBringer's avatar

@Shivamyadav I'm confused. Here

Route::get('/'

you do not accept params, so, here

function (Category $category)

you have dependency injection - new empty category. So, of course, it's empty, so

$currentCategory->name

returns null. it's expected behavior. To have current category you must accept it as param in your route

Route::get('/{category}'

this way here

function (Category $category)

it will be model binding which returns current category

Please or to participate in this conversation.