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

mhballa's avatar

laravel error undefined variable

I have been following Laracast 30 day to learn Laravel, everything was going great until ep5 when we started with props result in an error : Undefined variable $active

@props(['active'])

<a  class="{{ $active ? 'bg-gray-900 text-white':'text-gray-300 hover:bg-gray-700 hover:text-white rounded-md'}} rounded-md  px-3 py-2 text-sm font-medium"
    aria-current="{{ $active ? 'page':'false'}}"
    {{$attributes}}
    
>{{ $slot}}</a>

in the tag active='foobar' I have no idea where the problem is ,is it laravel or the code? using laravel 8.3 in docker.

0 likes
8 replies
Snapey's avatar

Why are you running 8.3. Are you sure you don't mean php 8.3? Laravel version is important because @props has not been around long.

Show how you use this component. active should be true or false.

1 like
mhballa's avatar

@Snapey Thanks for replay, yes , it is php 8.3. laraval is 11.9 as I said , this is following tutorial 30 day to learn laravel. the code above is in nav-link.blade.php file which is in a components file with layout.blade.php where the attribute active="foobar". exactly as the ep5 in the tutorial . what active should be true or false? active here is a props link the attribute in anchor tag. I have followed the tutorial closely. but it always result in laravel break with error Undefined variable $active.

JussiMannisto's avatar

@mhballa Post your code. People won't find the video and watch through it just to see what you're talking about. It wouldn't help anyway, because the bug is in your code, not the video.

The bug is that you're trying to use an undefined variable called $active. People can only help you fix it if they see your code.

1 like
mhballa's avatar

@JussiMannisto here it is

@props(['active'])

<a  class="{{ $active ? 'bg-gray-900 text-white':'text-gray-300 hover:bg-gray-700 hover:text-white rounded-md'}} rounded-md  px-3 py-2 text-sm font-medium"
   aria-current="{{ $active ? 'page':'false'}}"
   {{$attributes}}
   
>{{ $slot}}</a>
JussiMannisto's avatar

@mhballa You just reposted what you already showed. You need to show how you're using the component, and how you're passing the active prop to it.

Mega_Aleksandar's avatar
Level 18

Hi,

Are you properly passing in the prop from the view that uses the component? Also, if you are following the video, it also has a default value of false.

So, watch it again and again if you need to, there is no penalty for that.

Best regards

1 like

Please or to participate in this conversation.