Sortable on null id in laravel
It might be because you aren't protecting agaisnt csrf, which is mandatory when doing a post request so, as laravel docs explain you could solve by adding this to your html:
<meta name="csrf-token" content="{{ csrf_token() }}">
Then in your ajax you would only need to append it as a header.
$.ajax({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, url: '/categorysort/'+ itemID , type: 'POST', dataType: 'json', data: {itemID: itemID, itemIndex: itemIndex}, });
Also you are only recieving 1 id so I guess that what you are trying to do is sort depending on the parent, therefore you should only select the li
with parent class, that way your controller will recieve 1 id instead of 2 because what you are doing right now is /categorysort/{parentId}/{childId}
and what you would need is /categorysort/{id}
, so instead of selecting all categories just select the top parent categories:
$.map($(this).find('.parent'), function(el)
After see your comments about encodeURI(itemID)
, I know the problem is your route.php
.
I'm getting 404 error on network
You need to update your route with optional parameters :
Route::post( // Add {second_id?}. This is an "optional parameter". 'categorysort/{first_id}/{second_id?}', 'CategoryController@UpdatecategoryparentByAjax')->name('categorysort');
So, you can access :
mydomain.test/categorysort/1
mydomain.test/categorysort/1/2
If you need 3rd id
, add more optional parameters like this :
Route::post( 'categorysort/{first_id}/{second_id?}/{third_id?}', 'CategoryController@UpdatecategoryparentByAjax')->name('categorysort');
So, you can access :
mydomain.test/categorysort/1
mydomain.test/categorysort/1/2
mydomain.test/categorysort/1/2/3
If you want to make first_id
do the same thing, just add ?
after first_id
like {first_id?}
.
My function doesn't support null category_id
After update your route, your just need call :
public function UpdatecategoryparentByAjax(Request $request, $first_id, $second_id, $third_id){ // $first_id access your 1st ID // $second_id access your 2nd ID // $third_id access your 3rd ID // Do some logic here..}