You are not passing $currentCategory here?
return view('posts' , [
'posts' => $author->posts,
'categories' => Category::all()
]);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
@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>
Please or to participate in this conversation.