RomkaLTU
RomkaLTU

Reputation: 4129

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';
}

Upvotes: 12

Views: 33100

Answers (7)

Rafael Nobre
Rafael Nobre

Reputation: 71

I had the same problem. What was happening was a difference between the variable i set in the route and the variable i was receiving in the delete method

For example: my route was like this

...
Route::resource('global', InsumoController::class);
...

The resource create default routes, and one of them is:

global/{global} receiving the DELETE method

In the Controller, the variable that the method destroy receive must have the name 'global'. If not, will not work;

That worked for me, maybe is something to check if you have a similar issue.

Upvotes: 0

pableiros
pableiros

Reputation: 16022

My problem was, the name of the property inside the uri on the routes file was incorrect:

Route::delete('/foo/{fo3o}', [DollyController::class, 'delete']);
                  -----^

I don't know why the controller method was working:

public function delete(Foo $foo)
{
    $foo->delete();
    return ['message' => 'success'];
}

After correcting it, the problem was solve and the delete method started working again:

Route::delete('/foo/{foo}', [DollyController::class, 'delete']);
                ------^

Upvotes: 1

Tee&#39;s Touch
Tee&#39;s Touch

Reputation: 55

 DB::table('pykcodes')
                ->where('user_id', $user_id)
                ->delete();

This works directly without using the model. Just make sure you call use DB; from the top.

Upvotes: 1

Pradeep Kumar
Pradeep Kumar

Reputation: 31

Please check your model class if you have added soft delete option in that case your record will not be deleted from the database.

Upvotes: 3

RomkaLTU
RomkaLTU

Reputation: 4129

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'

Upvotes: 14

Julius Garcia
Julius Garcia

Reputation: 1

You have to add SoftDeletes in User Model:

...

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable {

use SoftDeletes;

protected $dates = ['deleted_at'];

...

Upvotes: -5

Don&#39;t Panic
Don&#39;t Panic

Reputation: 14510

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.

Upvotes: 1

Related Questions