1

I have been working on building a simple table that gets its data from a mongodb database that is being queried for the json data and not the data needs to be passed to the vue component on the frontend but I am unable to understand how the json data is transferred from laravel to vue. To understand the code here is what I have done so far:

api.php route:

Route::middleware('api')->group(function () {
Route::resource('/home', [ProductController::class,'home']);
});

web.php route:

Route::get('/home', [PostController::class,'home']);

PostController Home method:

public function home() {
    $posts = Post::paginate(4);

    return response()->json($posts);
}

Home component:

<template>
<div>
        <table>
            <tr>
                <td>Id</td>
                <td>Title</td>
                <td>Status</td>
            </tr>
         <tr v-for="El in results" v-bind:key="El.id" >
             <td>{{El.id}}</td>
            <td>{{El.STATUS}}</td>
        </tr>
        </table>
        
</div>
</template>

<script>

export default{
     data() {
        return {
            results: []
        };
    },
    methods: {
        fetch() {
            axios.get('/api/home')
                .then(response => this.results = response.data)
                .catch(error => console.log(error));
        }
    }
}
</script>
Home.blade.php

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row justify-content-center">
         
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Data</div>
                     
                <div class="card-body">
                  <home-component></home-component>
                </div>
                 
            </div>
        </div>
    </div>
</div>
@endsection 

All of this seems fine but when I run the code it just show the json and does not render any of the vue component. Any help is appreciated.

3
  • What is your question? Your vue script (frontend javascript) calls an URL which points to your server, which returns a JSON response which your vue script can directly use. Commented May 21, 2022 at 10:41
  • Yeah so it seems like all of it should work right? But when I go to /home route it just shows the json data. Commented May 21, 2022 at 10:48
  • Ah gotcha, ive posted an answer Commented May 21, 2022 at 10:53

1 Answer 1

1

You are using the /home URL both as a data endpoint and an HTML endpoint. What you should do is design it something like this:

Route: GET /api/home
Returns: JSON data
Code: `return response()->json(...);`

Route:: GET /home
Returns: HTML data with javascript that requests /api/home
Code: `return view('home');`

Both routes are ordinary http requests, but one only serves JSON data and the other one serves HTML data to be interpreted by your browser.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried what you said and I get an error saying` ErrorException Array to string conversion ` why is that I am not sure where I am converting array to string? also I have updated the question to include the new api route which I added
Also as a side note I am assuming something is wrong with how I am retrieving and reading the data as when I load the web page without the /api/home in the vue component all the table titles and table headings load in properly
Sorry I didnt get a notification for your comment. It's a bit hard for me to debug your errors like this; the first one is pretty much as it says: somewhere you are trying to print/output an array (probably in your view?). Just look at the error message where it points to. If it points to a weird file like 327xn23jk....php, then its pointing to a compiled view file. In that case, look at the correct line in storage/framework/views/327xn23jk....php.
Hi thank you fro your comment but I have actually moved on from here and I am currently working on a different problem but thank you for trying to help

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.