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

CamKem's avatar
Level 10

Help with Undefined variable error (laravel 8 from scratch)

Hello, I am following along the tutorial for laravel 8 from scratch and have run into a problem I can figure out.

In Episode 32 we are taking a layout that has been made by Jeffrey and are cutting it up into component & making them dynamic. The video jumps at the point he is getting the second component set up, and then when he fast forwards, his example is working fine. This is to save time on the repetition on the video. I did as he said and repeated the steps done on the post-card-feature component and I am getting an undefined variable error. I cannot work out why.

I have the following code in my main posts blade view

            <x-post-card-feature :post="$posts[0]"/>

            <div class="lg:grid lg:grid-cols-2">
                @foreach ($posts->skip(1) as $post)
                    <x-post-card-grid :post="$post"/>
                @endforeach
            </div>

Then a snippet of code from the two components are the following:

post-card-feature.blade.php - this works fine

@props(['post'])
<article
    class="transition-colors duration-300 hover:bg-gray-100 border border-black border-opacity-0 hover:border-opacity-5 rounded-xl">
    <div class="py-6 px-5 lg:flex">
        <div class="flex-1 lg:mr-8">
            {{-- TODO: Add image --}}
            <img src="../images/illustration-1.png" alt="Blog Post illustration" class="rounded-xl">
        </div>

        <div class="flex-1 flex flex-col justify-between">
            <header class="mt-8 lg:mt-0">
                <div class="space-x-2">
                    <a href="{{ route('category', $post->category->slug) }}"
                       class="px-3 py-1 border border-blue-300 rounded-full text-blue-300 text-xs uppercase font-semibold"
                       style="font-size: 10px">{{ $post->category->name }}</a>
post-card-grid.blade.php - this component is throwing the error

@props(['post'])
<article
    class="transition-colors duration-300 hover:bg-gray-100 border border-black border-opacity-0 hover:border-opacity-5 rounded-xl">
    <div class="py-6 px-5">
        <div>
            {{-- TODO: Add image --}}
            <img src="../images/illustration-1.png" alt="Blog Post illustration" class="rounded-xl">
        </div>

        <div class="mt-8 flex flex-col justify-between">
            <header>
                <div class="space-x-2">
                    <a href="{{ route('category', $post->category->slug) }}"
                       class="px-3 py-1 border border-blue-300 rounded-full text-blue-300 text-xs uppercase font-semibold"
                       style="font-size: 10px">{{ $post->category->name }}</a>
                </div>

The second component (post-card-grid.blade.php) is the part that is giving me an error, it is saying

Undefined variable $post

Please help

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

It looks correct. Does it point to where the error is ? Can you share the error page?

CamKem's avatar
Level 10

@Sinnbeck The error is coming up for each $post variable in the post-card-grid.blade.php. If I remove the line that has the first variable & refresh it just jumps the error down to the line with the next variable. I tried to ddd the $post variable in the view & for obvious reason it did not work. I also tried changing the variable name being passed through to the blade incase this error was due to the duplicate use of $post in passing the same variable name to each blade component, but this had no effect.

Here is a link to the shared error page: (https://flareapp.io/share/B5Z83YnP#F65)

Thanks in advance for your help.

Let me know if you need any other data,

CamKem's avatar
Level 10

@Sinnbeck I redid the tutorial & episode 31 too back to where this code started to be implemented and it works now. Not sure where the error was, but its all good as its working now.

Please or to participate in this conversation.