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

Doofy's avatar
Level 1

Blade component - access to the object

Hi, I don't have much experience with Laravel, but I want to learn a lot. I'm trying to access a variable in a Blade component and I keep getting error messages: "Undefined variable $currentLanguageColumn".

In the menu.blade.php component I have this code:

@props(['menuItems', 'currentLanguageColumn'])
<ul>
    @foreach ($menuItems as $menuItem)
    {{-- {{ dd($menuItem->{$currentLanguageColumn}) }} --}}

        <li>
            <a href="{{ route('menu.show', ['menuName' => $menuItem->id]) }}">{{ $menuItem->{$currentLanguageColumn} }}</a>

            @if (count($menuItem->subItems) > 0)

                <x-menu :menuItems="$menuItem->subItems" />

            @endif
        </li>
    @endforeach
</ul>

View welcome.blade.php contains this code:

@extends('layouts.app')

@section('content')
{{-- {{ dd(request()) }} --}}
    <h1>hello</h1>

    <x-menu :menuItems="$menuItems" :currentLanguageColumn="$currentLanguageColumn"/>

@endsection

and web.php contains this code:

Route::get('/', function () {

    $menuItems = request()->attributes->get('menuItems');
    $currentLanguageColumn = request()->attributes->get('currentLanguageColumn');
    // dd($currentLanguageColumn);
    return view('welcome', compact('menuItems', 'currentLanguageColumn'));
})->middleware(MenuDataMiddleware::class);

And now for the problem, when I try to access the object in this part of the code, I get an error message:

"Undefined variable $currentLanguageColumn"

This message comes from the menu.blade.php component, line:

<a href="{{ route('menu.show', ['menuName' => $menuItem->id]) }}">{{ $menuItem->{$currentLanguageColumn} }}</a>

So I tested the availability of the object using the dd() command.

@props(['menuItems', 'currentLanguageColumn'])

<ul>
    @foreach ($menuItems as $menuItem)

    {{ dd($menuItem) }}

        <li>
            <a href="{{ route('menu.show', ['menuName' => $menuItem->id]) }}">{{ $menuItem->{$currentLanguageColumn} }}</a>

            @if (count($menuItem->subItems) > 0)

                <x-menu :menuItems="$menuItem->subItems" />

            @endif
        </li>
    @endforeach
</ul>

I am getting a listing of the object. This object contains all the attributes I need.

#attributes: array:11 [▼
    "id" => 1
    "menu_id" => 1
    "parent_id" => null
    "title_en" => "Item 1 EN"
    "title_sk" => "Položka 1 SK"
    "url" => "/polozka-1"
    "icon" => "icon"
    "is_active" => 1
    "position" => 1
    "created_at" => "2023-10-27 06:43:32"
    "updated_at" => "2023-10-27 06:43:32"
  ]

I continue testing and access the attribute directly through the $currentLanguageColumn variable, which contains the name of the attribute: "title_sk" // resources/views/components/menu.blade.php

@props(['menuItems', 'currentLanguageColumn'])

<ul>
    @foreach ($menuItems as $menuItem)
    {{ dd($menuItem->{$currentLanguageColumn}) }}

        <li>
            <a href="{{ route('menu.show', ['menuName' => $menuItem->id]) }}">{{ $menuItem->{$currentLanguageColumn} }}</a>

            @if (count($menuItem->subItems) > 0)

                <x-menu :menuItems="$menuItem->subItems" />

            @endif
        </li>
    @endforeach
</ul>

And I successfully access the attribute "title_sk":

"Položka 1 SK" // resources/views/components/menu.blade.php

But when I try to access this attribute in the element in this way, I get an error message:

Undefined variable $currentLanguageColumn
 <a href="{{ route('menu.show', ['menuName' => $menuItem->id]) }}">{{ $menuItem->{$currentLanguageColumn} }}</a>
@props(['menuItems', 'currentLanguageColumn'])

<ul>
    @foreach ($menuItems as $menuItem)

        <li>
            <a href="{{ route('menu.show', ['menuName' => $menuItem->id]) }}">{{ $menuItem->{$currentLanguageColumn} }}</a>

            @if (count($menuItem->subItems) > 0)

                <x-menu :menuItems="$menuItem->subItems" />

            @endif
        </li>
    @endforeach
</ul>

Could someone advise me where I am doing wrong? Thank you very much.

0 likes
1 reply
Doofy's avatar
Doofy
OP
Best Answer
Level 1

I've already figured out where the error is. The error was in calling

<x-menu :menuItems="$menuItem->subItems" />

again after evaluating the:

@if (count($menuItem->subItems) > 0)

                <x-menu :menuItems="$menuItem->subItems" />

@endif

I forgot to forward the variable $currentLanguageColumn there. My fault.

This is the fixed code.

@props(['menuItems', 'currentLanguageColumn'])

<ul>
    @foreach ($menuItems as $menuItem)

        <li>
            <a href="{{ route('menu.show', ['menuName' => $menuItem->id]) }}">{{ $menuItem->{$currentLanguageColumn} }}</a>

            @if (count($menuItem->subItems) > 0)

                <x-menu :menuItems="$menuItem->subItems" :currentLanguageColumn="$currentLanguageColumn" />

            @endif
        </li>
    @endforeach
</ul>

Please or to participate in this conversation.