Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place vue variable inside a laravel bracket

I am having problem displaying the vue variable output when using it together with laravel. Below is my code.

<input type="text" 
               class="form-control search-bar__input" 
               placeholder="Search Courses, Interests, Schools or Institutions" 
               autocomplete="on" 
               autofocus="on"
               v-model="query"
               v-on:keyup="search">
<ul class="list-group">
    <li class="list-group-item" v-for="course in courses">
        <a href="{{ route('courses.show', '{{ course.id }}') }}">@{{ course.title }}</a>
    </li>
</ul> 

In the above code I make used of @{{course.id}} inside the route in the a element. However it seems it is not working. Can anyone help me on this.

like image 890
Jearson Avatar asked Sep 08 '16 11:09

Jearson


People also ask

How do I declare a variable in the method VueJS?

if you define global variable then you can easily access global variable in vuejs app. We will define global variables using vue js mixin. using mixin we will declare all variables in data method. then we will use in our app vue object.

Can I use Vue with Laravel?

Yes, you can use Laravel with Vue js. Both of them support single page applications, and this combination allows you to be a full stack developer within a single project.

How do I pass PHP variable to Vue component?

Also, you are using Vue's v-bind shorthand in :clientId="{{ $client->id }}" which means that Vue will deal anything inside double quotes as a JavaScript expression, therefore you may get errors in that case as well. Instead, you should use this format clientId="{{ $client->id }} without a colon. Save this answer.


1 Answers

You're mixing up server side and client side code there. You're passing @{{course.id}} as a parameter to a PHP function, so you'll probably be getting a syntax error.

You should use Laravel to output your route and then in your HTML/Blade you can output a Vue variable using the @{{ }} syntax.

<a href="{{ route('courses.show') }}?course_id=@{{ course.id }}">@{{ course.title }}</a>

That will generate a URL like http://yourdomain.com/courses/path/ and stick ?course_id= on the end of it.

My guess is that you're trying to use a route with a parameter in it, rather than a query string. In which case, you should try something like this (Not tested):

<a href="{{ route('courses.show', '') }}/@{{ course.id }}">@{{ course.title }}</a>

Note that I have put quotes ' around the second parameter to route and I have omitted the @ since we're hoping to directly output {{ course.id }} and we're in PHP, not Blade.

With any luck your URL will be generated as something like this:

http://yourdomain.com/courses/path/{{ course.id }}
like image 101
Jonathon Avatar answered Oct 10 '22 12:10

Jonathon