halivert / laravel-js-routes

Command for use Laravel routes in JS
https://halivert.dev/laravel-js-routes
MIT License
11 stars 6 forks source link

using post method doesn't work #11

Closed pishguy closed 3 years ago

pishguy commented 3 years ago

with this package i try to use post method for a single route, but in laravel side i get 'undefined for data

route:

Route::prefix('{language}')->middleware(['setLanguage'])->group(function () {
    Route::prefix('panel')->group(function () {
        Route::get('/user-profile', UserProfileController::class)->name('user-profile');
    });
});

route.js

const routes = {
    //
    "update_profile": {
        "uri": "{language}\/panel\/update_profile"
    },
    //
};
//...

vuejs axios impleemntation:

let formData = new FormData();
formData.append('name', this.name)
formData.append('family', this.family)
formData.append('website', this.website)
formData.append('telegram_id', this.telegram_id)
formData.append('instagram_page', this.instagram_page)
formData.append('avatar', this.avatar)

axios.post(route('update_profile'), formData, {
    headers: {"Content-Type": "multipart/form-data"},
}).then((result) => {
    console.log(result);
}).catch((error) => {
    console.log(error);
})

and output which laravel return for me:

Illuminate\Http\JsonResponse {#772
  #data: ""undefined""
  #callback: null
  #encodingOptions: 0
  +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag {#777
    #computedCacheControl: array:2 [
      "no-cache" => true
      "private" => true
    ]
    #cookies: []
    #headerNames: array:3 [
      "cache-control" => "Cache-Control"
      "date" => "Date"
      "content-type" => "Content-Type"
    ]
    #headers: array:3 [
      "cache-control" => array:1 [
        0 => "no-cache, private"
      ]
      "date" => array:1 [
        0 => "Sat, 03 Apr 2021 04:16:12 GMT"
      ]
      "content-type" => array:1 [
        0 => "application/json"
      ]
    ]
    #cacheControl: []
  }
  #content: ""undefined""
  #version: "1.0"
  #statusCode: 200
  #statusText: "OK"
  #charset: null
  +original: "undefined"
  +exception: null
}

laravel controller action:

public function update_profile(Request $request)
{
    dd(response()->json($request->input('avatar')));
}

error:

Missing parameters

halivert commented 3 years ago

In this line you should add the language key, or an extra parameter, remember that this function behaves as route function

axios.post(route('update_profile', {"language": "en"}), formData, {