Sunpower's avatar

Why did not delete DB record in Laravel?

working with Laravel 5.6 and I am going to delete table record from uploads images table. this is controller

public function deleteOneImage($id)
    {
        Upload::where('id', $id)

                ->delete();
return redirect()->back()->with('info', 'Image deleted successfully');
    }

and delete link in edit.blade.php

@foreach( $vehicles-> uploads as $upload)

  <a class="button is-outlined" href="/myads/{{$upload->id}}/delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a></td>

 @endforeach

and route

Route::get('myads/{uploads}/delete', [
    'uses' => '\App\Http\Controllers\VehicleController@deleteOneImage',
]);

but when I clicked delete buttons it is not delete the table records? how can I fix this problem?

0 likes
10 replies
tisuchi's avatar

In your route, if you update the code like this, it should work.

Route::get('myads/{uploads}/delete', 'VehicleController@deleteOneImage');
1 like
Snapey's avatar

As we ALWAYS say here, NEVER delete using a simple a link and GET request

Where is your check that the user is ALLOWED to delete the upload?

The reason it does not work is because the parameter in your route is uploads but your method is expecting $id

They should be the same (but without $ in the route)

Sunpower's avatar

@snapey Please see my updated Controller and route here

 public function deleteOneImage($uploadId)
    {

        Upload::where('id', $uploadId)
                
                ->delete();
 
 
        return redirect()->back()->with('info', 'Image deleted successfully');
    }

and route

Route::get('myads/{uploads}/delete', [
    'uses' => '\App\Http\Controllers\VehicleController@deleteOneImage',
]);

but this is same results not delete records

Snapey's avatar

because you still dont have the same name in the route and in the method signature

deleteOneImage($uploadId)

Route::get('myads/{uploads}/delete'

uploads versus uploadId

tykus's avatar
tykus
Best Answer
Level 104

The name of the route wildcard and action argument only matters whenever you want route-model binding, otherwise you can name the wildcard orange and the action method $apple if you wanted.

Do you know that you are actually reaching the correct Controller action; that the correct route is being matched? Do you have another route that could be matched before your get to the route you are showing us, e.g. Route::get('myads/{something_else}/delete', ...? I ask because the Upload is not the expected resource of a myads endpoint.

Also, what @Snapey mentioned earlier about as GET route for a non-safe action is sage advice, follow it!!!

Sunpower's avatar

@tykus I helped your this canswer

Do you have another route that could be matched before your get to the route you are showing us, e.g. Route::get('myads/{something_else}/delete', ...?

please can you describe the wrong practice of this two routes

Route::get('myads/{uploads}/delete', [
    'uses' => '\App\Http\Controllers\VehicleController@deleteOneImage',
]);

and

Route::get('myads/{vehicles}/delete', [
    'uses' => '\App\Http\Controllers\VehicleController@deleteOneAd',
    
]);
tykus's avatar

The problem is Laravel does not know whether myads/123/delete/ should delete a Vehicle or an Upload - how can it know whether 123 means a Vehicle id or and Upload id? So it choses the first route that matches. You should instead organise your routes so that it can uniquely identify the resource your want to show, or edit or delete, e.g.

Route::delete('myads/vehicles/{vehicles}', [
    'uses' => '\App\Http\Controllers\VehicleController@deleteOneAd',
]);

and

Route::delete('myads/uploads/{uploadId}', [
    'uses' => '\App\Http\Controllers\UploadController@deleteOneAd',
]);

Please or to participate in this conversation.