0

I am using Laravel 6 and VUE. I am converting a blade file into a VUE component. The blade file contains a table with rows of data being pulled in from a mysql DB. I have created a draggable component that is displaying users and will allow me to drag and drop rows in order to rearrange them, and the table is using VUE slots to pass html to my component. On my index view the table has a button on it. When the button is clicked it goes to a /create endpoint/route that I defined in Laravel using a routes.php file. I don't want to use vue routing, I prefer to maintain my laravel routes as they are. The issue I am having is, I do not know how to implement this exactly as I am new to VUE. Can someone shed some light on this? I tried using axios but maybe I am mis-using? Is there a better way to use a vue component but continue to use my routes I have defined in routes.php? Thank you.

main.blade.php

 <!-- Draggable Component -->
            <div id="app">
                <draggable table=@yield('content.main')></draggable>
            </div>

routes.php (partial to show which endpoint I am wanting to hit)

  $route->any('users/create', 'UsersController@create');

Draggable.vue

<template>
    <section v-bind:table="table">
        <button class="btn btn-default" @click="create()">
            <i class="fa fa-plus"></i>
        </button>
        <slot></slot>
    </section>
</template>

<script>
   export default {
       props: ['table'],
       data() {
           return {
               draggable: '',
           };
       },
       created() {
           this.draggable = this.table;
       },
       methods: {
           create() {
               axios.get(this.endpoint, {
                   UserName: this.UserName
               }).then(res => {
                   this.creating = true;
                   this.UserName = res.data.UserName
               }).catch(err => {
                   console.log("Error: " + err);
               })
           },
       },
       computed: {
           endpoint() {
               return `users/create`;
           },
       }
   };
</script>

The error I get when the button is clicked:

 Error: Request failed with status code 404
2
  • If you want to use vue with your laravel route then, combine the blade and the vue. Commented Jul 10, 2020 at 20:46
  • Hi @JesusErwinSuarez can you please elaborate? Or provide an example or point me to the correct docs? Thank you. Commented Jul 10, 2020 at 20:51

1 Answer 1

1

in your endpoint() you can add window.location.protocol + "//" + window.location.host so the method would be

endpoint() {
    return window.location.protocol + "//" + window.location.host + 'users/create';
},
Sign up to request clarification or add additional context in comments.

6 Comments

I added this in my endpoint method. The page is not redirecting to the /create page though?? In my browser console I do receive a 200 response and in the preview tab within the Network tab, the /create page is displayed. But in the actual browser itself, no REDIRECT is happening so the pages are not changing... why is this?
OK just to be clear, what do you want is when you press the create button you need to be redirected to /users/create and fetching data from this endpoint ?
Sorry if I am confusing. What I want is: when the button is clicked, I want to just redirect to the /create page.
Ok in this scenario you don't need the axios call as you not fetching any data, you need to use window.location = window.location.protocol + "//" + window.location.host + 'users/create'; and i think you can do it inline also.
inside your create method add window.location = this.endpoint(); only
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.