0

I am working on livewire in my project and I encounter a problem.

I have livewire model and blade file as below:

LivewireCounter.php

class LivewireCounter extends Component
{
    public $count;
    public function render()
    {
        return view(Constant::LIVEWIRE_COUNTER_BLADE);
    }

    public function mount($count = 0)
    {
        $this->count = $count + 1;
    }
    public function increment()
    {
        if($this->count >= Constant::LIVEWIRE_COUNTER_MAX)
        {
            $this->count = Constant::LIVEWIRE_COUNTER_MAX;
        } else {
            $this->count++;
        }
    }
    public function decrement()
    {
        if($this->count <= Constant::LIVEWIRE_COUNTER_MIN)
        {
            $this->count = Constant::LIVEWIRE_COUNTER_MIN;
        } else {
            $this->count--;
        }
    }
}

livewire-counter.blade.php

    <div>
    <div class="flex inline-block relative w-64">
        <button class="px-1" wire:click="decrement">-</button>
        <h1>{{ $count }}</h1>
        <button class="px-1" wire:click="increment">+</button>
    </div>
</div>

I have a coupon blade file which uses livewire to track the coupon ordered by user.

coupon-show.blade.php

    @extends(Constant::LAYOUTS_APP)

@section(Constant::LAYOUTS_CONTENT)

<div class="container">
            <div>
                <p class="text-4xl text-extrabold text-gray-900">{{ $CouponSetting->name }}</p>
                @livewire('livewire-counter', ['count' => 9])
            </div>
        {{$count}}
    </div>
</div>

@endsection

The problem I am having is I am unable to retrieve the $count value in the coupon-show.blade.php file. I thought the public property of the livewire component should be able to be accessed by blade view?

ErrorException Undefined variable: count (View: /Applications/MAMP/htdocs/XXX/resources/views/layouts/coupons/coupon-show.blade.php)

Any idea why?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
KWCham
  • 11
  • 4
  • 6
  • 1
    `$count` is only defined within the component, not outside it. So in your `coupon-show` view, the variable is not defined. – Qirel Oct 10 '20 at 15:21
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Qirel Oct 10 '20 at 15:22
  • Hi Qirel, thanks for the replies. Regarding the $count does not defined outside the livewire component, is there a way to pass the $count to outside component? I've checked a lot of examples on internet but none of them show me how to do this. – KWCham Oct 11 '20 at 15:46
  • 1
    Components - not just Livewire ones (its based on the Laravel Blade components) - are scoped. You can only access variables from within that component. You can declare a variable in the parent component, and fire an event to set the count on the parent component. – Qirel Oct 11 '20 at 16:00

0 Answers0