mushood's avatar
Level 41

Vue and laravel named routes

Hello

I am working on a vue project where I have a form:

Form:

<form method="POST" action:"/blog" @submit.prevent="submitForm">
.
.
.
.
</form>

The form is in a .vue file . Now I would like to change the action attribute to a named route in laravel. Here is my web.php route:

Route::post('/blog', 'BlogController@store')->name('blog.store');

However if I try this:

<form method="POST" action:"{{route('blog.store')}}" @submit.prevent="submitForm">
.
.
.
</form>

I get this error: Property or method "route" is not defined on the instance but referenced during render.

Which is understandable.

So my question remains, how to use named routes from laravel in vue

0 likes
20 replies
tisuchi's avatar

You have to define a variable in vue and assign route value on that. For example-

var app = new Vue({
  el: '#your-wrapper',
  data: {
    routeName: "{{route('blog.store')}}"
    .
    .

Now in your form tag, you can use like this way-

 <form method="POST" :action="routeName" @submit.prevent="submitForm">
1 like
mushood's avatar
Level 41

@tisuchi

This code is in my app.js file

var app = new Vue({
  el: '#your-wrapper',
  data: {
    routeName: "{{route('blog.store')}}"

and i cant move it to my blade file since I am importing js plugins

is there a way to add to the data object from the blade file ?

tisuchi's avatar

@mushood

I see..

Than simply you can do this-

 <form method="POST" :action="{{route('blog.store')}}" @submit.prevent="submitForm">

It should work for you.

2 likes
mushood's avatar
Level 41

@tisuchi Yep and I got this error during compilation:

 - invalid expression: Unexpected token { in

    {{route('blog.store')}}

  Raw expression: :action="{{route('blog.store')}}"
mushood's avatar
Level 41

@tisuchi

In my view instance,

routeName:"{{route('blog.store')}}"

its evaluating to the string ""{{route('blog.store')}}""

tisuchi's avatar

I see...

What if you think of a environment variable for that and apply like this-

{{ env('BLOGSTORE') }}

In your .env file -

BLOGSTORE=http://localhost/your-project/url-for-blog-store
3 likes
mushood's avatar
Level 41

I see where my problem is,

I am trying to pass a "php variable" to a "js variable"

Even if i set it in an ENV, it would still be the same issue.

Maybe i could set up an ajax request to get the URL. Seems tedious.

Or maybe pass the value in a hidden span and then retrieve it

@tisuchi What do you think?

Either way, I think I have to rethink my workflow

1 like
robrogers3's avatar
Level 37

@mushood using an ajax request to get the route is a bit like pulling your pants down to fart. especially if it's 1 route.

but if you need to pass it, you could pass it as a prop.

so in your blade file you would have.s

<my-component :route={{route('foo')}}></my-component>

now the issue here is that it doesn't like you are using a component. and that's dead dead simple.

in app.js

Vue.component('my-component', require('./MyComponent.vue')); //globally registered.
const app = new Vue({
    el: '#app'
});

in MyComponent.vue

<template>
    <form :action="this.route">
    <p>{{message}}
    </form>
</template>

<script>
 export default {
     props: ['route'],
     data() {
     return {
         message: ''
         stuff: [],
         
     }
     }

 }

if it's a nested component, i.e. a component inside the component passing routes around is a pain. if so, let me know and I'll show you other options.

13 likes
insanealec's avatar

I'll usually pass that kind of thing as a prop, or if the Vue file is too deep in then I'll have a hidden element within blade set to that value, and on mounting that component read the value of the element.

1 like
mushood's avatar
Level 41

Hi @MorganRowse It seems that passing the route as a prop is the way to go for now, as mentioned.

@robrogers3 Haha. You're right. Thank you for the code sample. Really helps out. I'll definitely check out vuex as well.

However, on the project I am working on right now, I am using the vue router. So in my blade, I only have :

      <router-link to="/create">
          <a><button type="button" class="btn btn-success">Create action</button></a>
      </router-link>
.
.
.
<div class="container">
  <router-view></router-view>
</div>

and then all my vue logic is in JS file. So i'm having to hard code my URLs at the moment.

At the same time, as my project is growing, I find the need to have a setup function where I do an ajax request to get the initial information from the server when a router-view is mounted. I'm thinking of simply passing my routes in this function.

Scoop7's avatar

what about moving my navigation sidebar from laravel blade to a vue component ? for href I had all named laravel routes, passing one by one as a prop to component would be soooo redundant :D Any advices ? Pass it as an array or smth ?:D

1 like
zethzethzeth's avatar

The fart-remark made me fall down my chair in laughter.

1 like
bionary's avatar

Oh man I've been laughing for 10 straight minutes over the fart comment. My wife thinks I'm a lunatic right now!

1 like

Please or to participate in this conversation.