Voyager: Soft Delete for Users

Created on 29 Apr 2019  路  14Comments  路  Source: the-control-group/voyager

Version information

Description of problem

I have a system which includes a lot of user. A user has a project, if the admin deleted the user from User's table, project created and user should still be indicated as author. Already added deleted_at on the users table and added soft delete to User Model, still no luck.

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends \TCG\Voyager\Models\User
{
    use HasApiTokens, Notifiable, SoftDeletes;

    protected $dates = ['deleted_at'];
feature

Most helpful comment

Nevermind what's written above is going to give you AppUser.

The problem is VoyagerBaseController uses what's in database data_types.model_name and that's, by default, set as TCGVoyagerModelsUser, so unless you changed it that's the model used for users as BREAD.

BTW

Try checking in BREAD -> users -> Model Name

That would have already solved your problem yesterday :wink:

All 14 comments

Did you add withTrashed() to belongsTo relationship?

Did you add withTrashed() to belongsTo relationship?

@MrCrayon

I didn't. Is it needed to add ?
btw, It's the default user model I'm using .

As per Laravel documentation, yes.
https://laravel.com/docs/5.8/eloquent#soft-deleting

@MrCrayon

Sorry sir, but I think you're talking about retrieving the trashed data.
My problem is the data is not being soft deleted.

Maybe rephrase your issue because that's what it sounds like, if user is deleted and not soft deleted then that's the problem I would not even mention any other relationship where the user doesn't show up :wink:

Also if this is the problem it should be marked as possible BUG not feature.

Anyway I just tried on a fresh install and it works, are you sure Voyager is set to use your User Model and not the default one?

Try checking in BREAD -> users -> Model Name

@MrCrayon

I tried using fresh install. Model is correct.
Still, deleting user was not soft deleted.

Do you see this in user list?
Schermata da 2019-05-02 11-55-33
If you don't see the third button soft delete is not enabled.

Can you list everything you do after fresh install to enable soft delete?

@MrCrayon

I'm not seeing that one. What did i missed ?

already added on User Model.
image

The only way I can explain it is if that class is not the one used.

Try adding route below in your route.php

Route::group(['prefix' => 'admin'], function () {
    Voyager::routes();

    Route::get('/users', function () {
        return get_class(auth()->user());
    });
});

Then go to /admin/users you should see which class you are using

Nevermind what's written above is going to give you AppUser.

The problem is VoyagerBaseController uses what's in database data_types.model_name and that's, by default, set as TCGVoyagerModelsUser, so unless you changed it that's the model used for users as BREAD.

BTW

Try checking in BREAD -> users -> Model Name

That would have already solved your problem yesterday :wink:

@MrCrayon

Thank You! :)

You are welcome!

Please close the issue if it has been solved.

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zzpwestlife picture zzpwestlife  路  3Comments

rayqiri picture rayqiri  路  3Comments

iwasherefirst2 picture iwasherefirst2  路  3Comments

vaggelis2018 picture vaggelis2018  路  3Comments

popica80 picture popica80  路  3Comments