Livewire: protected props get null value after submit or any action

Created on 9 Nov 2020  Â·  3Comments  Â·  Source: livewire/livewire

Description

if I declare a protected var like :: protected $categories;

protected $categories;
public function mount(Category $categorie )
{
$this->categories= $categorie;

}

in render method $this->categories still has data
public function render()
{

    return view('livewire.product.products', [
         'categories'=>   $this->categories->all(),

    ]);
}

when I call {{dd($categories)}} in blade view('livewire.product.products') … Data it's still no problem here 💯

but when I call a method from submit Form example :

and inside submit() function I need $this->categories example ::

public function submit()
{
dd($this->categories ); return null
}
$this->categories return null after any action !!!

Exact steps to reproduce

solution I use is inject Model in function again but i don't like it !!!
public function submit(Category $categorie)
{
dd($categorie->all() ); return data
}

Stripped-down, copy-pastable code snippets

Context

  • Livewire version: [e.g. 2.2.3]
  • Laravel version: [e.g. 8.3.1]
  • Alpine version: [e.g. 2.3.0]
  • Browser: [e.g. Chrome, Safari]

Most helpful comment

That is how it works, only public properties are accessible between requests. So you have to make $categories public if you want it accessible in your submit method.

See here in the docs https://laravel-livewire.com/docs/2.x/properties#important-notes

protected and private properties DO NOT persist between Livewire updates. In general, you should avoid using them for storing state.

Hope this helps!

All 3 comments

That is how it works, only public properties are accessible between requests. So you have to make $categories public if you want it accessible in your submit method.

See here in the docs https://laravel-livewire.com/docs/2.x/properties#important-notes

protected and private properties DO NOT persist between Livewire updates. In general, you should avoid using them for storing state.

Hope this helps!

👋 Oh Hi! I'm Squishy, the friendly jellyfish that manages Livewire issues.

I see this issue has been closed.

Here in the Livewire repo, we have an "issues can be closed guilt-free and without explanation" policy.

If for ANY reason you think this issue hasn't been resolved, PLEASE feel empowered to re-open it.

Re-opening actually helps us track which issues are a priority.

Reply "REOPEN" to this comment and we'll happily re-open it for you!

(More info on this philosophy here: https://twitter.com/calebporzio/status/1321864801295978497)

Thanks Josh!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oliverbj picture oliverbj  Â·  3Comments

cao-minh-duc picture cao-minh-duc  Â·  4Comments

roni-estein picture roni-estein  Â·  3Comments

pmartelletti picture pmartelletti  Â·  3Comments

zaherg picture zaherg  Â·  4Comments