I want to add a post in laravel livewire which needs to be look like if categories are not in dropdown then it should create a category with that name, similarly for tags i'm beginner to livewire please help me how to achieve that without page loading on final submit.
I have tried
function mount(){
$this->categories= Categories::all();
$this->category = $this->article->category;
$this->post_tags = $this->article->tags;
$this->tags = Tags::all();
-------
--------
--------
}
function store(){
$this->article->description = $this->description;
----
----
----
}
But I'm not getting categories and tags into store method by '$this->category' and '$this->post_tags' when changing in form. this is returning only the null or when editing the old values from db not the form values
To get the dropdown value in livewire, you will need to change the select to include wire:model
and add a public property on the class
Next, you can use the Laravel Collections method ::contains
to see if the category exists, if not create.
Should look something like this:
new class extends Component
{
public ?Collection $categories = null;
public string $category = '';
...
public function mount() {
$this->categories = Categories::all();
}
public function store() {
if (!$this->categories::contains($this->category)) {
// Category does not exist, create
...
}
}
}
...
<div>
<select id="category" wire:model="category"...>
...
</select>
</div>