skip to Main Content

I am trying to achieve a show and hide component using livewire and laravel. But first I need to make a global variable to make a toggle true or false value whenever a button is clicked thus showing or hiding a component. Whenever you run php artisan make:livewire (name of the component) there are two files that will be generated, the blade file and the php file:

In the php file whenever I write a code of:

use LivewireComponent;

class NavManager extends Component
{
    public $helloVariable = "hello";
    
  
    public function render()    
    {
        return view('livewire.nav-manager');
    }


}

and access the $helloVariable in the blade component file as:

<div>
    <li><a href="">Home</a></li>
    <li><a href="">Account</a></li>
    <li><a href="">About</a></li>

    {{$helloVariable}}
    
</div>

it returns an error of "Undefined variable $helloVariable"

I also tried displaying the $helloVariable at welcome.blade.php file it also returns the same error.

Is this a bug of livewire or laravel? How can I fix this?

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out, whenever I am calling the component I am using @include('livewire.navmanager') it gives the error of undefined variable but when I changed it into <livewire:navmanager /> it works well.


  2. From docs you supposed to do it like so:

    class ShowPosts extends Component
    {
        public function render()
        {
            return view('livewire.show-posts', [
                'posts' => Post::all(),
            ]);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search