Reputation: 7022
How can I update a record from Vue.js to Laravel ?
I am trying to send AJAX request from vue like below
var address_data = {
name :this.dname,
address :this.address,
telephone_no:this.telephone_no,
email :this.email,
image :this.image,
user_id :this.user_id,
}
this.$http.post('http://127.0.0.1:8000/api/addresses/{address}',address_data,{headers: {'Authorization': 'Bearer ' + this.$auth.getToken()}}).then(response => {
if(response) {
//some code
}
},
response => {
//some code
})
In api.php
my route is like below
Route::group(['prefix' => 'addresses', 'middleware' => 'auth:api'], function () {
Route::put('/{address}', 'AddressController@update');
});
In AddressContoller.php
I wrote below code for update
function.
public function update(Request $request, Address $address)
{
$address->update($request->all());
return response()->json($address, 200);
}
May be I did some mistake. I don't know where is the mistake.
Upvotes: 0
Views: 499
Reputation: 491
I think one of the problems there is that it won't find a route and methods. You have multiple problems there:
'
, you need to use ``, so your code sends request to http://127.0.0.1:8000/api/addresses/{address}
instead of http://127.0.0.1:8000/api/addresses/2
for exampleaddress
" is not defined.post
but on laravel you defined the route as a put
.Upvotes: 1