No query results for model, ajax single row delete Laravel 5.3?

Ajax query:
$(document).on('click', '.delete_single', function(){
 var id = $(this).data("id");
 var token = $(this).data("token");

$.ajax({
 url: "/product/"+id+"/delete",
 type: 'delete',
 data: {
 "id": id,
 "_method": 'delete',
 "_token": token,
},
 success: function ( response ){
 console.log("Success! Row deleted.");
},
 error: function(){
console.log("ERROR!");
},
});
$(this).parents('tr').remove();
 });

Routh
Route::delete( 'product/{product_id}/delete', 'ProductController@delete' );

Controller
public function delete(Product $product)
{
 if( exists( $product ) && !empty( $product ) )
{
$product->delete();
}
}

product_id saintil as a model.
Route::model('product_id ', Product::class);

How should I do?
July 2nd 19 at 18:19
1 answer
July 2nd 19 at 18:21
Solution
1. Not easier in .delete_single add attribute
data-delete="{{ route('products.delete', ['product' => $product]) }}"
and then use it in the query?
2. Why use the name of the product_id instead of product? Without this, you can use Route::resource
3. You with each request to prescribe a token? Maybe it's worth to know about the ajaxSetup?
4. Why data transfer _method id and if the id is in the link, and the query type is determined based on the type: "DELETE"?
5. exists( $product ) && !empty( $product ) - why is that? Laravel itself verifies the existence of a record with the passed id
6. Need something in response to give: return response()->json($product->delete())

If all of these comments to take into account, and the error will disappear, I think :)
1 - Read about AjaxSetup - Set default values for future Ajax requests. Its use is not recommended.
2 - always in response to something to return?
3 - "_method": 'DELETE' - wasn't working ( before saintil model).
4 - I don't quite understand how it to use?
data-delete="{{ route('products.delete', ['product' => $product]) }}" - jaycee commented on July 2nd 19 at 18:24
:
1. Instead, you can use the ajaxSetup https://api.jquery.com/ajaxSend/
2. Preferably, I do think that no response will be an error :)
3. Should work, as he did. To bandit model does not need to register in route /products/{product}/delete , and then in the controller: function destroy(Product $product) {
4. The route function creates a link to a named route pattern (here we need to specify the parameters, i.e. replace {product} on $product->id (by default id is used). - brando commented on July 2nd 19 at 18:27
Andrew Stark @BonBonSlick
1 -how are we using ajaxSend to send the token if it's a PhP function?
$( document ).ajaxSend(function() {
csrf_token();
});
2 - well ordered, and why is it? What to do with the response after his return?
3 - everything is good, so sales are easier, thank you!
4 -data-deleter="{{ route('product.destroy', ['product_id' => $product->product_id]) }}"
writes:
Route [product.destroy] not defined.
although ranting:
Route::delete( 'product/{product}/destroy', 'ProductController@destroy' ); - jaycee commented on July 2nd 19 at 18:30
: you need to first documentation to read, and then ask questions. - brando commented on July 2nd 19 at 18:33

Find more questions by tags AJAXjQueryPHPJavaScriptLaravel