Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maison012's avatar

How to pass json data from laravel controller to vue js

i am using vue js on my frontend and laravel for backend side. I have create a form when i can post data on db using axios to pass data on laravel controller. But what i am trying now is to pass data from laravel to vue js. I have done this in one way but i thisk there is another better way. Can some one help with this. My data can pass from laravel controller to vue js but every time i send new data i need to refresh page to see updates

  public function index()
    {
        $posts = Post::orderBy('id', 'DESC')->get();

        return view('Posts.index') ->with([
            "posts" => $posts
        ]);
    }
<div class="container">
            <div class="m-5">
                <form @submit.prevent="postForm()" id="signin" class="form-horizontal validate-form">
                    <div class="form-group">                
                        <input v-model="title" id="title" type="text" class="form-control" name="title" placeholder="Title">        
                    </div>        

                    <div class="form-group">  
                        <textarea v-model="comment" id="comment" type="text" class="form-control" name="comment" placeholder="Comment"> </textarea>
                    </div>          

                    <div class="form-group mb-0">    
                        <input type="submit" value="Submit" class="btn btn-info" />
                    </div>
                </form>
            </div>

            <div class="m-5" v-for="(posts, index) in posts" :key="index">
                <div class="card m-0 col-12">
                    <div class="card-header">
                        {{posts.title}}
                    </div>
                    <div class="card-body">
                        {{posts.comment}}
                    </div>
                    <div class="card-footer">
                        <form @submit.prevent="deletePost(posts.id)" action="" method="POST">
                            <input class="d-block btn btn-danger" type="submit" value="Delete" />
                        </form>
                    </div>
                </div>
            </div>

        </div>
import axios from 'axios'

export default {
    props:['posts'],

    data() {
        return {
            title: '',
            comment: '',
        }
    },
    
    methods: {
        postForm() {
            axios({
                method: 'post',
                url: '/post',
                data: {
                    title: this.title,
                    comment: this.comment,
                }
            }).then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });
        },

        deletePost(id) {
            axios({
                method: 'DELETE',
                url: '/post/{post}' +id,
            }).then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });
        }
    },

}
// index.blade
//i just set this on `div container`
<post-data :posts="{{$posts}}"></post-data>
0 likes
9 replies
vincent15000's avatar

You have to use Laravel in API mode and your controller returns JSON datas instead of collections.

And from VueJS you use the Laravel API routes to get the datas you need.

Maison012's avatar

@vincent15000 Why should i use lareavel api. I think there is any way to use larvael normal route for this

1 like
vincent15000's avatar

@usertxr API routes are normal routes, generally with api/ but it's not obligatory.

By saying to use Laravel in API mode, I meant your controller have to return JSON data and not redirect to a view.

Maison012's avatar

@tisuchi I have tryed these. And it works. I can get data from db on vue throught laravel controller. What i want is, when i press submit button posts will automaticly render and will add on the post section without refresh. If i press submit now with @submit.prevent="postForm()" function i need to reload manually. If i remove .prevent works great, post will render automatically but i see an update on my url => http://127.0.0.1:8000/post?title=Quibusdam+sint+quis+&comment=Voluptatem+porro+vel and i think url should be only http://127.0.0.1:8000/post and axios resquest should stay on network tab only.

1 like
vincent15000's avatar
Level 63

@usertxr Effectively you don't use vuex.

The idea is to do something like this.

        postForm() {
            axios({
                method: 'post',
                url: '/post',
                data: {
                    title: this.title,
                    comment: this.comment,
                }
            }).then(function (response) {
                console.log(response);
				// RELOAD YOUR DATAS
            })
            .catch(function (error) {
                console.log(error);
            });
        },

That's not so easy to do.

You will quickly realize that vuex is very useful.

You should follow some courses about VueJS on Laracast.

https://laracasts.com/series/learn-vue-3-step-by-step

yudhees's avatar

@maison012 Try to use this package composer require yudhees/laravel-vue-controller github url "https://github.com/yudhees/laravel-vue-controller/"

By this package you don't need to write a apis to access the controller from the vue , u just pass the controller path and your function name , also u can send params to the function from the vue to controller

Please or to participate in this conversation.