Clear cache
php artisan cache:clear
php artisan view:clear
php artisan config:clear
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 -->
@alinandrei What if you change nav.php to be Nav.php and also change the component class name to be capitalize class Nav?
If you're on windows, I recommend to delete the file and create it with proper names again.
Or try to register it manually
https://laravel.com/docs/9.x/blade#manually-registering-components
Please or to participate in this conversation.