Search code examples
databaselaravelauthenticationreferencelaravel-jetstream

How do I delete a certain field in a different table when the user deletes his account?


I have a problem when a user deletes his account, but a list that he created still exists, how do I delete the list when the user deletes his account? I use jetsream laravel for auth.

The following is the list table that I want to delete based on the email to be deleted:

image.png

and the following is the users table that you want to delete:

image.png

I have tried reading the jetsream docs, but still can't find a solution, please help friends.


Solution

  • If you are saving the lists of a user in another table, then you just need to take the user id as a foreign key in the lists table and use onDelete('cascade') on that list table.

    For example:

    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')
    ->on('users')>onDelete('cascade');
    

    When a user will delete his account, then the associated data with his account will also be deleted in the list table.