Reputation: 1589
I'm trying to attempt to delete the user from my database as well as their profile. Right now it is only deleting the row in the users table. The only difference here is that the user_profiles table is marked as the user_id field which is NOT the increment field. How should this be accomplished? Is this something that should be wrapped in a transaction?
/**
* Remove the specified resource from storage.
*
* @param int $id id
*
* @return Response
*/
public function destroy($id)
{
$this->userRepository->delete($id);
$this->userProfileRepository->delete($id);
return redirect('users');
}
Upvotes: 1
Views: 85
Reputation: 111859
Yes, you can use transaction to make sure both records will be deleted but you could also create foreign key for user_id
field in user_profiles
table with ON DELETE CASCADE
and it will be automatically removed so you don't need to run delete on user_profiles
manually.
For example in your migration file it could look like:
$table->foreign('user_id')->references('id')>on('users')->onDelete('CASCADE');
Upvotes: 2