[Vuejs]-Axios – Redirect to controller with data

0👍

if i understand correctly your problem is that you don’t see the return of “dd”.

It’s normal your console.log is after the window! and no dd with axios! you have to return to see the answer!

Controller

 public function create(Request $request)
{
     return $request['ProductId'];
}

.vue

/*js codes*/
        Submit : function(){
            axios.post('/buy',{
                ProductId : this.id,
                //ProductId : "1";//
                ProductAmount : this.temporaryamount
                //ProductAmount : "12000";//
            })
                .then(function (response) {
                    console.log(response);
                    //Remove window.location, or if you dont remove you dont see console.log!
                    //window.location = "/buy";

                })
                .catch(function (error) {
                    console.log(error);
                })
        }

and on your route remove get.

/* web.php */
Route::post('/buy','ShoppingController@create');

0👍

Basically, you cannot used together those two create functions with different route method and at the same time you want to get the product id. Your post request will work on that situation but the get request will not. that’s why dd($request[‘ProductId’]) is null. You cannot use request as a parameter whenever you use a get request. $request as a parameter is for post request only.

for further explanation please read laravel docs source

so to solve your problem, you may remove this line

window.location = "/buy";

and show your post data in the controller by this

public function create(Request $request) {
   dd($request->all())
}

Leave a comment