Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete a user in Laravel (eloquent) without its relations?

I have a laravel application and I want to delete the user record form users table but keep the data related to them like articles.

I have this function for article

public function userArticles()
    {
        return $this->hasMany('Application\Model\userArticles');
    }

I want to delete the user without deleting the articles from database, but when I do $user->delete() it deletes everything.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

is there a way to keep the articles?

>Solution :

UPDATE YOUR MIGRATION ->onDelete(‘set null’)
It means when the user was deleted, the field will be null.

$table-> bigInteger('user_id')->unsigned()->nullable()->index();
$table->foreign('user_id')->references('id')->on('admins')->onDelete('set null');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading