mafortis
mafortis

Reputation: 7138

How to delete user without posts in Laravel?

I'm working on L5.5 and I need to delete user but not his/her posts. So I basically need to assign his/her posts to another user which has to be Non-removable.

What I need:

  1. Create a user which can't be deleted at least not from front-end even by owner of website but can be edited. (mostly is like bot for this application)
  2. If I delete a user and that user had post(s) those post(s) remain and assign to this user (bot). It means this bot will become author of those posts.
  3. Check for number 2 that only if user with post that happens if user has no post just delete him/her.

This is my usecontroller destroy method currently:

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');
    }

Thanks.

Upvotes: 0

Views: 1105

Answers (2)

sid heart
sid heart

Reputation: 581

Edit your database with

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

Upvotes: 0

Nitish Kumar
Nitish Kumar

Reputation: 6286

According to your needs, you will require softDeletes in your User model and their respective tables in the database, now this solves your 1st problem where your not deleting the user from table simply adding deleted_at column.

Edit: As you are using Zizaco\Entrust\Traits\EntrustUserTrait you need to have your user model look something like this:

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();
    }
}

For more information about this error you need to look: https://github.com/Zizaco/entrust/issues/742

so now coming to the 2nd point, retrieving the post with deleted model can be used withTrashed() something like:

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

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

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.

Edit:

So in this case you can have:

$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();
}

Now for your 3rd point if the user has no post then you can do a small check something like this:

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

Hope this justifies all your needs.

Conclusion So fnally you can have your destroy method in userController as:

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');
}

Upvotes: 1

Related Questions