alinandrei's avatar

Undefined Variable in Component

Hello,

I am missing something. Hopefully you can clear things for me.

It goes like this.

I have a nav component, NavController under App\View\Components\nav.php


class nav extends Component {


    /**
     * The user.
     *
     * @var object
     */
    public $user;


    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct() {
        $this->user = Auth::user();
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render() {
        return view('components.nav');
    }
}

Within the component's blade under resources/views/components/nav.blade.php, I have a user manangement section like so:

<div class="user-management-container">
    @if(is_null($user))
                    <button id="dropdown-no-user" class="
                        text-slate-700
                        dark:text-slate-100
                        focus:outline-none
                        font-medium rounded-lg text-sm px-4 py-2.5 text-center inline-flex items-center"
                        type="button">Sign in / Register
                    </button>
    @else
         <!-- Some code -->
    @endif
</div>				

NOTE: The error persists if I'm using isset within the blade's @if condition;

NOTE: I have also tried the following code, with no different result: Of course, I have removed the property and the constructor property binding from above when trying it this way.

public function render() {
		$user = Auth::user();
		
        return view('components.nav')->with('user', $user);
    }

I have read as per the documentation, public properties of components are automatically injected in the template; However, I have tried to do it with ->with('user', $user), but with no success.

The nav component is brought up inside a general view, welcome.blade.php as .

I am using Laravel 9.35.1 and PHP 8.1.11, and I receive ErrorException: Undefined variable $user. Why?

Thanks in advance.

Additional Informations:

Route for Home: Route::get('/', [ HomeController::class, 'render' ]);

HomeController:

class HomeController extends Controller {

    public function render(): \Illuminate\Contracts\View\View
    {
        return view('pages.home');
    }
}

views/pages/home.blade.php:

@extends('welcome')

@section('content')
    <!-- Homepage content -->
@endsection

views/welcome.blade.php

<!-- HTML Boilerplate -->
<main class="h-screen">
            <header class="bg-white dark:bg-slate-800">
                {{-- Navigation components.nav --}}
                <x-nav></x-nav>
            </header>

            <div class="content bg-white dark:bg-slate-800 ">

                @yield('content')

            </div>

            {{-- Footer components.footer --}}
            <x-footer></x-footer>
        </main>
<!-- More HTML Boilerplate -->
0 likes
12 replies
MohamedTammam's avatar

Clear cache

php artisan cache:clear
php artisan view:clear
php artisan config:clear
1 like
Shivamyadav's avatar

try with changing this

public function render() {
		$user = Auth::user();
		
        return view('components.nav', compact('user'));
    }
Shivamyadav's avatar

@alinandrei Try changing @if(is_null($user)) to empty

<div class="user-management-container">
    @if(empty($user))
                    <button id="dropdown-no-user" class="
                        text-slate-700
                        dark:text-slate-100
                        focus:outline-none
                        font-medium rounded-lg text-sm px-4 py-2.5 text-center inline-flex items-center"
                        type="button">Sign in / Register
                    </button>
    @else
         <!-- Some code -->
    @endif
</div>				
Shivamyadav's avatar

change this also just for testing purpose ```Checks that is it gives you the same error or working properly.

public function render() {
		return "Working";
		//Else try this 
$user = "Hello";
return view('component.nav', compact('user'));
    }
AlfredHsu's avatar

I faced the same issue with Laravel 11 components. To resolve this, make sure your class name and file name follow the Camel-Case naming convention. Incorrect naming may lead to errors.

Here’s my example:

  • Original component file name: tokenGenerator.php → changed to TokenGenerator.php
  • Original class name: tokenGenerator → changed to TokenGenerator

After making these changes, clear your view cache by running:

php artisan view:clear

I hope this helps you resolve the issue! 😊

Please or to participate in this conversation.