【问题标题】:How to delete user without posts in Laravel?如何在 Laravel 中删除没有帖子的用户?
【发布时间】:2017-09-15 02:47:35
【问题描述】:

我正在开发 L5.5,我需要删除用户但不是他/她的帖子。所以我基本上需要将他/她的帖子分配给另一个必须是不可移动的用户。

我需要什么:

  1. 创建一个用户,即使是网站所有者也不能从前端删除,但可以编辑。 (主要是这个应用程序的机器人)
  2. 如果我删除了一个用户并且该用户有帖子,那么这些帖子将保留并分配给该用户(机器人)。这意味着该机器人将成为这些帖子的作者。
  3. 检查 2 号,仅当用户有帖子时才会发生这种情况,如果用户没有帖子,则删除他/她。

这是我目前使用的控制器销毁方法:

public function destroy($id)
    {
        $user = User::findOrFail($id);
        Storage::delete($user->image);
        $user->delete();
        return redirect()->route('users.index')->with('flash_message', 'User successfully deleted');
    }

谢谢。

【问题讨论】:

  • 你的模型中需要softDeletes laravel.com/docs/5.5/eloquent#soft-deleting
  • @NitishKumar 感谢兄弟,这更像是建议,即使我使用软删除,我也需要回答我的其他需求,例如将帖子分配给其他用户等?

标签: php laravel laravel-5


【解决方案1】:

根据您的需要,您将需要 softDeletes 在您的 User 模型中以及它们各自在数据库中的表,现在这解决了您没有从表中删除用户只需添加 deleted_at 列的第一个问题。

编辑:当您使用Zizaco\Entrust\Traits\EntrustUserTrait 时,您需要让您的用户模型看起来像这样:

class User extends Model implements AuthenticatableInterface
{
    use Authenticatable;
    use EntrustUserTrait { restore as private restoreA; }
    use SoftDeletes { restore as private restoreB; }

    public function restore()
    {
        $this->restoreA();
        $this->restoreB();
    }
}

有关此错误的更多信息,您需要查看:https://github.com/Zizaco/entrust/issues/742

所以现在来到第二点,可以使用withTrashed() 来检索已删除模型的帖子,例如:

$user = User::withTrashed()->all();
$user = User::withTrashed()->where('id', 1);

$posts = $user->posts()->get();
// Or do your relational things

即使您想将其分配给不同的用户,您也需要创建一个新用户并将更新方法应用于所有关系模型,同时删除该用户,这似乎是个坏主意。

编辑:

所以在这种情况下,您可以:

$oldUser = User::find($id);
$user = User::find($botID); // Find the bot user

$oldFoods = $oldUser->food()->get();
foreach($oldFoods as $food)
{
    $food->user_id = $user->id;
    $food->save();
}

现在对于您的第三点,如果用户没有帖子,那么您可以做一个小检查,如下所示:

$user = User::find($request->id);
$posts = $user->posts()->get()->first();
if(isset($posts))
{
    $user->delete();
}
else
{
    $user->forceDelete();
}

希望这能满足您的所有需求。

结论 所以最后你可以在userController 中拥有你的destroy 方法:

public function destroy($id)
{
    $user = User::findOrFail($id);
    $foods = $user->food()->get();
    if(isset($foods))
    {
       $botUser = User::where('username', '=', 'bot'); // Find the bot user
        foreach($foods as $food)
        {
            $food->user_id = $botUser->id;
            $food->save();
        }
        $user->delete();
    }
    else
    {
        $user->forceDelete();
    }
    Storage::delete($user->image);
    return redirect()->route('users.index')->with('flash_message', 'User successfully deleted');
}

【讨论】:

  • 嗨兄弟,谢谢你的回答我有一些问题,1.这是$user = User::withTrashed(); $posts = $user->posts()->get();的哪个方法? 2. WordPress 使用这个功能也不错Even if you want to assign it to different user then you need to create a new user and apply update methods to all the relational model while deleting the user which seems a bad idea. 我该怎么做?
  • @mafortis 用户和帖子之间的关系是什么?
  • 用户模特public function food(){ return $this->hasMany(Food::class); }美食模特public function user(){ return $this->belongsTo(User::class); }
  • 我认为它们在不同的控制器中,对吧?你能告诉我什么控制器和什么方法我应该放置你的代码来测试它吗? note 当我将 softdelete 添加到我的用户模型时,我收到此错误 Trait method restore has not been applied, because there are collisions with other trait methods on App\User
  • ps:我有这个额外的特征use Zizaco\Entrust\Traits\EntrustUserTrait; 将它用于我的角色和权限。
【解决方案2】:

编辑你的数据库

$table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')
                    ->onDelete('restrict')
                    ->onUpdate('restrict');

【讨论】:

  • 在哪一部分?为什么?
猜你喜欢
  • 2019-12-01
  • 2018-11-12
  • 2021-10-19
  • 2017-10-05
  • 1970-01-01
  • 2015-04-22
  • 2021-03-01
  • 2017-10-30
  • 1970-01-01
相关资源
最近更新 更多