I have a database with users and a database with posts. A user has many posts. When a user deletes their account, I don’t want to have their posts deleted. This results in a problem: when I try to access name of the post author (like this: $post->user->name
), I get an error Attempt to read property "name" on null
. What I would like to get is something like "Deleted user". How can I achieve this?
2
Answers
You can depend on the relation from the eloquent only.
but Do not use foreign key To avoid sql problems
Here are the solutions …
You can delete the author admin using the soft delete
You can Show modal before deletion Confirms that this author has many active articles
You can transfer articles from one author to another. When the deletion command is executed, the alternative is specified by default in the method
Or it is determined from the confofim modal
You can make an accessor method in model
check if isset author relation if null return The alternative that matches your idea
Your problem is fixed :
Have a good day