1

I am trying to fetch data from database using vuejs (in laravel 8 framework ). But it yields no data, no error either. I am sharing some screenshots and codes here :

View

enter image description here

Database

enter image description here

web.php

Route::get('/','App\Http\Controllers\vuecrud@index');

Route::post('/app/add_data','App\Http\Controllers\adminController@addTag');

Route::get('/app/get_data','App\Http\Controllers\adminController@show');

Route::any('{slug}','App\Http\Controllers\vuecrud@index'); 

AdminController

public function show()
{  
   
   return admin::all();
   
}

admin(model)

class admin extends Model
    {
        //use HasFactory;
        protected $fillable = ['tagName'];
    }

app.js(inside js floder)

require('./bootstrap');

window.Vue = require('vue')
import router from './router'
import ViewUI from 'view-design';
import 'view-design/dist/styles/iview.css';
import common from './common'
Vue.mixin(common)

Vue.use(ViewUI);

('mainapp', require('./components/mainapp.vue').default)
const app = new Vue({
    el: '#app',
    router
})

router.js

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import firstPage from './components/pages/firstvuepage'
import aboutpage from './components/pages/aboutus'
import admin from './components/pages/admin'

const routes = [
    {
        path:'/firstpage',
        component: firstPage
    },
    {
        path:'/about',
        component: aboutpage
    },
    {
        path:'/admin',
        component: admin
    }
]

export default new Router({
    mode:'history',
    routes
}) 

admin.vue(where I am trying to fetch data from database )

<template>
  <div>
    <table  class="_table">
          <tr>
              <td>Name</td>
              <td>Action</td>
          </tr>
      
          <tr v-for="(getname, i) in admins" :key="i" v-if="admins.length">
              <td class="_table_name">{{getname.tagName}}</td>
              <td>
                  <Button class="btn btn-info" size="small">Edit</Button>
                  <Button class="btn btn-danger" size="small">Delete</Button>                 
              </td>
          </tr>
      </table>

  </div>

</template>



<script>
    export default {
        data (){
            return {
                data:{
                     tagName:''
                },
                modal1: false,
                isAdding: false,
                admins: []
            }
        
        },

        methods :{
       
            async created(){
                const res = await this.callApi('get','app/get_data')
                if(res.status==200) {
                    this.admins = res.data
                
                }else{
                    this.smr()
                }
            }
        }
    
    }

</script>

common.js file

export default {
data(){
    return{

    }
},
methods:{
    async callApi(method, url, dataobject){

        try{
          return await axios({
             method: method,
             url: url,
             data: dataobject
           });

        }catch(e){
          return e.response
        }
    }
}

enter image description here

enter image description here

18
  • Simplest way to check what data is being populated is via vue-devtools - value of admins. You can also check the data received by the axios request by inserting a console.log statement in the if(res==200) block console.log(res) and check the value in console Commented Nov 24, 2020 at 23:27
  • Try adding a forward slash to the url this.callApi('get', '/app/get_data') Commented Nov 24, 2020 at 23:33
  • not working , I get a waring though : it says "DevTools failed to load SourceMap: Could not load content for 127.0.0.1:8000/js/iview.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE" Commented Nov 24, 2020 at 23:57
  • 1
    Find admin component from Filter Components panel and then in below panel see what value does admins have. If it is not what you expect. Go to Network tab and find the entry for /app/get_data and look at the response received Commented Nov 25, 2020 at 1:02
  • 1
    admins property is an empty array - not as desired. You ned to check by console.log(res) and see the Network tab, find /api/get_data and check it's response. Need to ascertain whether data is received from the server as expected or not - dd(admin::all()) in controller - Need to find out where things are going wrong Commented Nov 25, 2020 at 2:06

1 Answer 1

1

I believe you want the created method to be called when the component is being created. Remove the created function from inside the methods property and add it to the exported default object.

export default {
    data() {...},
    methods: {...}, 
    created() {...}
}
Sign up to request clarification or add additional context in comments.

6 Comments

Also confirm that you registered the common.js file as a global mixin or register it in the admin.vue file
Thanks for your kind help! It's working after I put created method outside methods : {... } , but can you kindly tell me why It was not working inside the method : {...}
now the problem is - I am to refresh the page to see whether the data inserted or not
created() is a lifecycle hook for vue component, it runs automatically during the lifecycle of a vue component. Where as the ones within methods:{} need to be run explictly
do you have any solution to make created() run inside methode:{...} ?
|

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.