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

kenshin9's avatar

Why does delete() method on model return null?

I'm currently using Laravel 5.1.11. When I use the delete() method on a model, it's returning null instead of a boolean. Looking at the code for the Model class, I don't see why it would return null. I'm using dd() to check the value returned from the delete, so something like dd($model->delete()).

Would anyone know why it's not returning a boolean?

0 likes
6 replies
usman's avatar

@kenshin9 when we try to delete a newly created model that is not yet saved to the database, the null value is returned. It is because the first condition is bypassed because the getKeyName returns the primary key column name:

if (is_null($this->getKeyName())) { // the getKeyName returns the primary column name.
        throw new Exception('No primary key defined on model.');
}

So, no exception is thrown here. Also, the model does not exists yet the other condition is also bypassed hence the null value as result is received on the calling side.

Usman.

thomaskim's avatar

It does return a boolean as you can see here:

/**
 * Delete the model from the database.
 *
 * @return bool|null
 * @throws \Exception
 */
public function delete()
{
    if (is_null($this->getKeyName())) {
        throw new Exception('No primary key defined on model.');
    }
    if ($this->exists) {
        if ($this->fireModelEvent('deleting') === false) {
            return false;
        }
        // Here, we'll touch the owning models, verifying these timestamps get updated
        // for the models. This will allow any caching to get broken on the parents
        // by the timestamp. Then we will go ahead and delete the model instance.
        $this->touchOwners();
        $this->performDeleteOnModel();
        $this->exists = false;
        // Once the model has been deleted, we will fire off the deleted event so that
        // the developers may hook into post-delete operations. We will then return
        // a boolean true as the delete is presumably successful on the database.
        $this->fireModelEvent('deleted', false);
        return true;
    }
}

It returns null if you are not actually deleting anything. For example:

$model = new Model;
$model->delete();

That returns null because no record exists in the database.

kenshin9's avatar

Sorry, I wasn't clear in my original question. I do have a record in the database. The variable name was just an example. I actually did a findOrFail() for my record, and it does exist. But when I use the delete() method on it, it returns null. And it definitely gets deleted from the DB.

$task = Task::findOrFail(1); // task exists

dd($task->delete ()): // null, but gets deleted
kenshin9's avatar

Ah yes. Sorry for not following up, but I eventually figured it out. By default, it is supposed to work as I thought. But a member on the team overrode the delete() functionality, and did not return a value. So that's why I was getting null.

2 likes

Please or to participate in this conversation.