[Vuejs]-Send two or more variable from laravel controller to vue component

1๐Ÿ‘

โœ…

You can achieve this simply by using response method of laravel

public function something()
{
    $data=Model::all();
    $comments=Comment::all()
    return response()->json([
         'data'=> $data,
         'comments'=> $comments
       ], 200);
}

or the other way using same method

public function something()
{
        $data=Model::all();
        $comments=Comment::all()
        return response([
         'data'=> $data,
         'comments'=> $comments
        ], 200);
}

and in your component you can simply get data using

export default
{
    created()
        {
           axios.get("a url")
                .then(res=>{
                       console.log(res.data.data)
                       console.log(res.data.comments)
                    }
        }
}

Thanks

๐Ÿ‘คSalman Zafar

2๐Ÿ‘

Here you go you can create an associate array and send through json.

public function something()

    {
        $data=Model::all();
        $comments=Comment::all()
        return response()->json([
         'data'=>$data,
         'comments'=>$comments
       ]) //here i want to send both $data and $comments to that view
    }

In Vue you can write something like this.

export default
{
    data(){
       return {
         comments:[]
       }
    },
    created()
        {

           axios.get("a url")
                .then(res=>{
                    this.comments = [...res.data.comments] //If you are using ES6
                    }
        }
}
๐Ÿ‘คMr.Throg

1๐Ÿ‘

You can simply do this

public function function()

{
    $data=Model::all();
    $comments=Comment::all()
    return response()->json([
     'data'=>$data,
     'comments'=>$comments
   ]) //here i want to send both $data and $comments to that view
}
๐Ÿ‘คPranta Roy

Leave a comment