You can not pass values from Javascript to already rendered blade template. Server first renders the blade and serves it to the browser, and only then the Javascript gets to work.
Dec 4, 2020
7
Level 12
Passing an alpine.js value to a blade component attribute
Hi! I'm new to Laravel and I wanted to make a blade component. The blade component receives a custom attribute. Basically, the component is working. But it won't work anymore if I fill the custom attribute with an alpine.js value.
<!-- resources/views/example.blade.php -->
<div x-data="{
myvalue1: 'hello',
myvalue2: {
mysubvalue1: 'foo',
mysubvalue2: 'bar'
}
}">
<div x-text="myvalue1"></div>
<div x-text="myvalue2.mysubvalue1"></div>
<div x-text="myvalue2.mysubvalue2"></div>
<x-example-comp valuethrough="a new value is given">My slot goes here !</x-example-comp>
<x-example-comp valuethrough="somethn else"></x-example-comp>
<!--- The problem is with the next line -->
<x-example-comp :valuethrough="myvalue1"></x-example-comp>
</div>
And here is the component:
<!-- resources/views/components/example-comp.blade.php -->
@props([
'valuethrough'=>'nothing'
])
<div>
Something new here.
<div>{{$slot}}</div>
<div>{{$valuethrough}}</div>
</div>
I know there are "conflict" with the blade component (using x- as prefix) and alpine.js. So, my main question is how is the best way to handle this, just passing an alpine.js javascript value (string or even better, an object) to a blade component attribute.
Thanks for your help, Basil
Please or to participate in this conversation.