Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brianbabu's avatar

destroy(id) is not working in Laravel 5.2

Newbie here at Laravel destroy(id) is not working in Laravel 5.2 :(

my controller function

public function destroy($id){
    /* Deleting a product */
    Product::destroy($id); // 1 way 
    return redirect()->route('product.index');
}

I tried using both the methods for deleting but it isn't deleting the records in MySQL although it does return with "true"

Is there any configuration am missing?

This is the form using which am initiating the delete..

{!!Form::open([
    'method'=>'delete',
    'route' =>['product.destroy',$product->ID]
])!!}
    <h1>{{$product->name}}</h1> <h3>{{$product->price}}</h3> <a href="#">Edit Product Info</a>
{!!Form::submit('Delete')!!}
{!!Form::close()!!}

Please Help! Thanks in Advance! Guys!

0 likes
23 replies
tykus's avatar

Very strange that something so fundamental would not be working. Just off the top of my head, what I'd check...

  • Have you logged your database queries and confirmed that there is a DELETE SQL statement?
  • Are you using a database transaction and not committing it?
  • Stupid question, it's got to be asked... you are looking at the correct database and the correct table?
Snapey's avatar

The only thing I can think is that you are not actually hitting the destroy method in your controller, or that the id is not being passed through correctly.

dd($id) just before the Product::destroy($id);

I checked similar in one of my projects (which I know works fine) (L5.0 though)

    {!! Form::open(array('route' => array('admin.templates.destroy', $template->id), 'method' => 'delete')) !!}
        <button class='btn btn-danger pull-right btn-xs' type="submit">Delete Template</button>
    {!! Form::close() !!}

brianbabu's avatar

@tykus_ikus : There should be a delete sql statement. I have used it in the past projects on CodeIgniter. It is looking at the correct database. In the same app, i was able to insert records.

I'm just learning the framework by watching youtube tutorials for CRUD app. It was going good until this happen.

@Snapey : When i added dd($id). It returned the id number as "7"

brianbabu's avatar

I tried destroy() and then i tried delete(), it didn't work!

public function destroy($id){
    /* Deleting a product */
    //Product::destroy($id); // 1 way 
    $product = Product::find($id) // another way
                ->delete(); 
//  Product::find($id)->delete();
    return redirect()->route('product.index');
}
brianbabu's avatar

Its happening for edit function as well! It isn't updating the record

public function update(request $request,$id)
{

/* inserting values to db - 1st way*/

    $product=Product::find($id);
    $product->name=$request->name;
    $product->price=$request->price;
    $product->save();
    return redirect()->route('product.index');
}    

This is so straight forward but i can't seem to figure out what's wrong here...

jekinney's avatar

I am guessing here but I would bet you may have a route collision. If your routes have product/{id} and product/{id}/update it will hit the first product/{id} it finds regardless of any use after it.

For me to always avoid this make sure it's a pit/patch and delete route. I also don't pass wild cards {id} except on a show route. Everything else is passed in session or the form to the controller.

1 like
cristian9509's avatar

@brianbabu Post your routes since as @jekinney said you might have an route collision issue. Also, did you logged what DB query is actually executed?

Also, public function update(request $request,$id) should be public function update(Request $request,$id) but I guess yo just wrote the code straight in here.

brianbabu's avatar

This is all i have in routes

Route::group(['middleware' => ['web']], function () { /* Route::resource(name, contrller-name, options); */ Route::resource('product', 'ProductController'); });

The 'R' in request didnt make any change either. I used 'request $request' for inserting the record and it worked just fine.

@cristian9509 : Could you pls tell me how to find out what DB query is actually executed?
Thanks.

I was following this (https://www.youtube.com/watch?v=5n6UgzhExAc&list=PL3ZhWMazGi9IcgWunA4izwTkd9QjSGW2j&index=8) tutorial series to learn laravel and it was for the 5.1 version. I got the 5.2 version but i was able to pull off everything in series except for edit and delete. :(

brianbabu's avatar

Additionally, I referred to the documentation as well but still hit a bump......

cristian9509's avatar

@brianbabu To get all the queries executed place this in your route's file

// Display all SQL executed in Eloquent
Event::listen('illuminate.query', function($query)
{
    var_dump($query);
});

Another way which I like better is to use Clockwork https://github.com/itsgoingd/clockwork. You will need to install a plugin, I recommend Chrome extension Clockwork and then you need to pull the clockwork package to your Laravel installation, add the service provider and the middleware and then you're all set. Just open Chrome developer tools, got to Clockwork tab, reload the page and you'll see all the DB queries there.

jekinney's avatar

@brianbabu

Clockwork is a must have BTW

Check your routes by running php artisan route:list

Screen shot it and post. Generally using route resource many devs use the actual url instead of route helper by name. But verify your action route is correct. If not fix them or post your route list and each form action (route) you're trying to post to.

johnDoe220's avatar
public function destroy($id)
{
	$product = Product::find($id);
	$product->delete();
	session()->flash('success', 'product deleted successfully');
	return redirect(route('products.index'));
}
Snapey's avatar

@johnDoe220 thanks for your contribution 6 years later! Most people would use route model binding, but why load the model into memory only to delete it?

Where are your authorisation checks?

What if this Id is invalid?

1 like
johnDoe220's avatar

@Snapey The time does not matter, the important thing is that if this question arises for someone, his problem will surely be solved Method route model binding is slow and should not be used anywhere. Deleting a product is done only by the admin, so why should the admin enter an ID that is not in the database? I think it is always better when the data is changed by the user Route model binding to be used Anyway, best way

public function destroy($id)
{
	$product = Product::findOrFail($id);
	$product->delete();
	session()->flash('success', 'product deleted successfully');
	return redirect(route('products.index'));
}
Sinnbeck's avatar

@johnDoe220 Why not just delete it directly instead of 2 queries?

$product = Product::where('id', $id)->delete();

And why is route model binding slower than loading it in the controller? This is news to me, but maybe I missed some benchmark

johnDoe220's avatar

@Sinnbeck When deleting a product, the tags should probably be removed as well as many other things like images or related videos So the right way, in my opinion, is to use two queries

johnDoe220's avatar

@Snapey My suggestion is to check the speed of each method.Of course, PHP language, thanks to its great flexibility, allows the programmer to do a task in different styles, and this is exactly where the opinions will be very different, in any case, anyone who can do a task in any way, but It will be very problematic in large applications

Snapey's avatar

@johnDoe220 again, rubbish. Your method is identical to using route model binding. I agree that using eloquent to delete the model gives opportunity for model observers to run and perform additional actions, but this is probably true only in a small number of cases and the developer will be aware of this and use the appropriate approach

1 like
johnDoe220's avatar

@Snapey When we do not use words like rubbish Better We can talk about these cases using the method Route model binding It is very wrong everywhere And that's entirely up to the programmer's attitude, good luck

waywardson's avatar

I had the same issue and still can't figure out why destroy isn't working, but I did:

Model::where('id', $id)->delete();

And that worked.

Snapey's avatar

@22289d destroy is a shorthand for where and delete

Model::destroy($id);

Please or to participate in this conversation.