nbbfv

How to delete Model without a primary key?

Posted 1 year ago by nbbfv

I have this model:

class EmailValidationToken extends Model
{
    protected $primaryKey = null;

    public $incrementing = false;

    public $timestamps = false;

    protected $fillable = [
        'email',
        'token'
    ];

    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->created_at = $model->freshTimestamp();
            $model->token = str_random(12);
        });
    }
}

When I try to delete one instance like this:

$emailValidationToken = EmailValidationToken::where('token', $token)->first();
$emailValidationToken->delete();

Tells me :

Exception in Model.php line 1100: No primary key defined on model.

    in Model.php line 1100
    at Model->delete() in PlayerController.php line 14
    at PlayerController->validateEmail('wSyr59KBGfmN')

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.