Reputation: 653
I can pass the data to component but in modal by Alpine js the data is null.
this is class:
public $code, $products;
public function getData($id)
{
$product = Product::find($id);
$this->code = $product->code;
}
public function render()
{
$this->products = Product::latest()->get();
return view('livewire.cabin');
}
and this is the component:
<div x-data="{open: false}">
<section>
if I use $code here the code value is shown !!!
<div>{{ $code }}</div>
<div class="slideCabin">
@foreach($products as $product)
<div>
<img
@click="open = true"
wire:click="getData({{ $product->id }})"
src="/images/allproducts/{{ $product->cover }}"
>
</div>
@endforeach
</div>
</section>
this is modal which is open by Alpine js by click on tag:
<div id="backmodal" x-show="open">
but, the code value is null:
<p>{{ $code }}</p>
</div>
Upvotes: 4
Views: 6684
Reputation: 63
To make it work you have to add some validation rules. use the "public function rules()" option
Upvotes: 0
Reputation: 815
you need to check if the code value has changed before calling it in the modal. you got null value because it's been called before assigning it any value.
@if($code)
<p> {{ $code }} </p>
@endif
Upvotes: 5