I have two models, Menu and User with many to many relationship. So, at this moment I have lot of records with the same user_id and menu_id in the pivot table, and the user must to can detach only one of them and not all of them.
So, I made a AJAX request to "remove" one menu from this table:
function deleteMenuFromOrder(menuFromOrderId){
event.preventDefault();
$.ajax({
type: "GET",
url: "/removeMenuFromOrder",
data: {
menuFromOrderId : menuFromOrderId,
userId : "{{$user->id}}",
_token : "{{csrf_token()}}"
},
success: function (response) {
alert(response);
// location.reload();
},
fail: function(response){
alert(response);
}
});
}
And this the "controller":
Route::get('/removeMenuFromOrder', function (HttpRequest $request) {
$user = User::findOrFail($request['userId']);
$user->menus()->detach($request['menuFromOrderId']);
return "Menú eliminado con éxito";
});
The menuFromOrderId is the id from pivot table which I want to delete.
Unfortunately, I can't to upload images on StackOverflow yet, but I have linked one about pivot table's data.
How you can see, I have lot of records with the same user_id and menu_id, if i detach by user_id, all records will be deleted and viceverse.
So, how can I detach only single record in this case?