Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass route parameter in a href using Alpine.js

I am using Alpine.js for the first time inside of a Laravel project and would like to add an a href tag to a component however I am unsure how to route the parameter correctly

<template x-for="(post, index) in posts" :key="index">
    <a x-bind:href="post.id">
        <p class="mt-2 block text-sm font-medium text-gray-900 truncate pointer-events-none">
            <span x-text="post.text"></span>
        </p>
    </a>
</template>

does redirect to

/id 

and passes the id but I need to go to the named route posts and expect

/posts/id

if I add

 <a x-bind:href="posts/post.id">

the result is a redirect with

/posts/NaN
like image 639
cinameng Avatar asked Jan 25 '26 08:01

cinameng


1 Answers

You can try this: <a x-bind:href="posts + '/' + post.id">

like image 110
Maik Lowrey Avatar answered Jan 26 '26 23:01

Maik Lowrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!