Home > Mobile >  Laravel BadMethodCallException on delete()
Laravel BadMethodCallException on delete()

Time:11-18

Unable to perform delete on find(),

BadMethodCallException Call to undefined method App\Models\Task::ondeletemodel().

But when using where it works.

    public function delete($id)
    {
            Task::find($id)->delete(); //error
            // Task::where('id', $id)->delete(); //not error 
            session()->flash('message', 'Task Deleted Successfully.');
    }

Edited : here is my uuid trait: <?php

namespace App\Http\Traits;

use Facades\Str;

trait UseUuid
{
    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->uuid = unique_str($model);
        });

        static::deleting(function ($model) 
        {
            $model->ondeletemodel($model);
        });
    }
}

CodePudding user response:

as it shows in the error message:

In the use App\Http\Traits\UseUuid probably you have defined a listener to be fired on ondeletemodel but there is not an method named like this, in the trait or the Task Model

CodePudding user response:

$tasks = Task::find($id);
$task->users->detach();
$tasks->delete();
session()->flash('message', 'Task Deleted Successfully.');
  • Related