Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent delete() not working

Documentation says:

$user = User::find($user_id);

$user->delete();

This doesnt work, however ProductColor::find($color_id) working. $color->delete() Doest return anything, DELETE FROM query doesn't even execute (as seen in debug bar).

But I can delete record with:

ProductColor::destroy($color_id);

Must be something I overlooked earlier, I'm new to Laravel.

I'm using store also, and it working as expected

public function store(Request $request)
    {
        $color = new ProductColor();
        $color->name = $request->color_name;
        $color->order = $request->color_order;
        $saved = $color->save();

        if ($saved) {
            return back()->with('message:success', 'Ok');
        } else {
            return back()->with('message:error', 'Error');
        }

    }

To sum up

This WORKS

public function destroy($color_id)
    {
        $deleted = ProductColor::destroy($color_id);

        if ($deleted) {
            return back()->with('message:success', 'Deleted');
        } else {
            return back()->with('message:error', 'Error');
        }
    }

This NOT

public function destroy($color_id)
{
  $color = ProductColor::find($color_id);

  $color->delete();
}

My Model

<?php

namespace Modules\Shop\Entities;

use Illuminate\Database\Eloquent\Model;
use Modules\Languages\Entities\Language;

class ProductColor extends Model
{
    protected $fillable = [];
    protected $table = 'product_colors';
}
like image 581
RomkaLTU Avatar asked Aug 13 '17 09:08

RomkaLTU


People also ask

How do I delete a record in laravel eloquent?

In Eloquent, you can delete database records conveniently with the delete method from the parent Model class. The link:delete command, already implemented within the base version of the demo application, deletes links based on a valid link id.

How to delete records in Laravel?

app/Http/routes.phpRoute::get('delete-records','StudDeleteController@index'); Route::get('delete/{id}','StudDeleteController@destroy'); Step 6 −The output will appear as shown in the following image. Step 7 − Click on delete link to delete that record from database.

What does eloquent delete return?

Delete method of query builder returns the number of affected rows. So, if no records deleted - 0. If 10 - 10.


2 Answers

Sorry, I've figured out the problem. My mistake to post this question.

What I tried to do:

$color = new ProductColor();
$color->find($color_id);
$color->delete();

Should be:

$color = ProductColor::find( $color_id );
$color->delete();

My problem was that I was scared about the IDE complaining about using non-static method 'find'

like image 177
RomkaLTU Avatar answered Oct 06 '22 01:10

RomkaLTU


Your code is fine - the docs show exactly what you are doing.

If there is no error, and the color is not deleted as expected, then $color_id is not being passed as expected. Try using findOrFail, or add some other check that you found the expected model.

like image 39
Don't Panic Avatar answered Oct 06 '22 03:10

Don't Panic