删除用户资料
Deletion of User Profile
我正在尝试从我的数据库中删除该用户及其个人资料。现在它只是删除用户 table 中的行。这里唯一的区别是 user_profiles table 被标记为 user_id 字段,它不是增量字段。这应该如何实现?这是应该包含在交易中的东西吗?
/**
* 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');
}
是的,您可以使用事务来确保删除这两条记录,但您也可以使用 ON DELETE CASCADE
为 user_profiles
table 中的 user_id
字段创建外键它将自动删除,因此您无需 运行 手动删除 user_profiles
。
例如,在您的迁移文件中,它可能如下所示:
$table->foreign('user_id')->references('id')>on('users')->onDelete('CASCADE');
我正在尝试从我的数据库中删除该用户及其个人资料。现在它只是删除用户 table 中的行。这里唯一的区别是 user_profiles table 被标记为 user_id 字段,它不是增量字段。这应该如何实现?这是应该包含在交易中的东西吗?
/**
* 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');
}
是的,您可以使用事务来确保删除这两条记录,但您也可以使用 ON DELETE CASCADE
为 user_profiles
table 中的 user_id
字段创建外键它将自动删除,因此您无需 运行 手动删除 user_profiles
。
例如,在您的迁移文件中,它可能如下所示:
$table->foreign('user_id')->references('id')>on('users')->onDelete('CASCADE');