laravel / nova-issues

553 stars 34 forks source link

[Enhancement proposal] Hidding the "With Trashed" checkbox #333

Closed laraning closed 5 years ago

laraning commented 6 years ago

I am using soft deletes on a model, but when I am refering that relationship I would like to hide the "With Trashed"checkbox from the Edit form.

Reason is I don't want the end user to understand this concept, but I still want to have soft deletes on the Model.

Maybe something like:

BelongsTo::make('VehicleType', 'type')
                ->disableTrash()

Is there a way to do it?

image

Thank you for the help!

bobinrinder commented 6 years ago

Totally agreed, we use soft deletes in a lot of places but the average user does not (need to) understand the concept and it would be good to hide it for them. To be flexible with permissions I would rather hope for a

BelongsTo::make('VehicleType', 'type')
                ->showWithTrashed(auth()->user()->isSuperAdmin())

that defaults to true or false if not given obviously

mateusgalasso commented 6 years ago

Some solution?

dillingham commented 6 years ago

+1 Im using soft deletes as a super admin feature to catch any admin mistakes

Here it is as a global config option in the NovaServiceProvider

Gate::define('viewNova', function ($user) {});
Gate::define('canSoftDelete', function ($user) {});

in Resource.php

public static function softDeletes()
{
    if (\Gate::check('canSoftDelete')) {
        return parent::softDeletes();
    }

    return false;
}
dkulyk commented 6 years ago

Better to use the withTrashed ability in the policy for each resource. This way you can issue permissions for each model separately.

In the my resource(I inherit base resource) I use this:

    public static function softDeletes()
    {
        return (static::authorizable() ? Gate::check('withTrashed',
                [static::newModel()]) : true) && parent::softDeletes();
    }
waygou commented 6 years ago

Works like a charm, thanks!

davidhemphill commented 5 years ago

Closing as the solution was posed above.

skalero01 commented 5 years ago

What if i'd like to hide the withTrashed option only on the edit page? I dont want to hide the filter, does somebody know? :D

davidhemphill commented 5 years ago

You could potentially check the current URI using the request() helper, but it'd be a bit of a workaround.

bkilshaw commented 5 years ago

Is it not possible to chain a ->hideWithTrashed() like we do with ->hideFromIndex() ?

loveyuxiao commented 4 years ago

hope add disableTrashed(true|false) just like R64\NovaFields\BelongsTo

briavers commented 4 years ago

This worked for me. (guess it was added into nova) so I'll leave it here for others who might find this thread

BelongsTo::make('order')
    ->withoutTrashed(),
didix16 commented 4 years ago

@briavers but not on BelongsToMany ... I hope they add also in BelongsToMany too.

tpetry commented 4 years ago

@didix16 At the moment you can hide it with some custom css in your meta.blade.php:

<style>
    label[dusk$="with-trashed-checkbox"] {
        display: none;
    }
</style>
didix16 commented 4 years ago

@tpetry ok thanks. I'll use it till they add the functionality for BelongsToMany.

genesiscz commented 4 years ago

Any news?

KiddoLin commented 4 years ago

Hi~ The BelongsToMany field has not withoutTrashed method, how to filter the pivot that use SoftDeletes? thx~ nova version: v2.9.3

ricardov03 commented 4 years ago

+1 Im using soft deletes as a super admin feature to catch any admin mistakes

Here it is as a global config option in the NovaServiceProvider

Gate::define('viewNova', function ($user) {});
Gate::define('canSoftDelete', function ($user) {});

in Resource.php

public static function softDeletes()
{
    if (\Gate::check('canSoftDelete')) {
        return parent::softDeletes();
    }

    return false;
}

Thanks, this option work great for me.

ricardov03 commented 4 years ago

Better to use the withTrashed ability in the policy for each resource. This way you can issue permissions for each model separately.

In the my resource(I inherit base resource) I use this:

    public static function softDeletes()
    {
        return (static::authorizable() ? Gate::check('withTrashed',
                [static::newModel()]) : true) && parent::softDeletes();
    }

@dkulyk I'm relative new on Nova but a fast learner, I really want to understand your solution, looks simpler but it doesn't work for me when I try to use it.

genesiscz commented 4 years ago

Better to use the withTrashed ability in the policy for each resource. This way you can issue permissions for each model separately. In the my resource(I inherit base resource) I use this:

    public static function softDeletes()
    {
        return (static::authorizable() ? Gate::check('withTrashed',
                [static::newModel()]) : true) && parent::softDeletes();
    }

@dkulyk I'm relative new on Nova but a fast learner, I really want to understand your solution, looks simpler but it doesn't work for me when I try to use it.

What field do you want to put this on?

ricardov03 commented 4 years ago

I'm using the solution shown by @dillingham. I would like to understand how can I use instead my Policy File to do the same job and without touch the core files of Nova.

This is my current configuration: File: app\Providers\NovaServiceProvider.php (Nova core file)

Gate::define('canSoftDelete', function ($user) { 
    return $user->is_admin(); \\ is_admin() it's a custom method in my User Model file to confirm if the current user it's admin.
});

File: app\Nova\Resource.php (Nova core file)

public static function softDeletes()
{
    if (\Gate::check('canSoftDelete')) {
        return parent::softDeletes();
    }

    return false;
}

I would like to use just the Policy file of my Model to do the same.

LiamKarlMitchell commented 1 year ago

Apologies for necro Was looking for a way to do this in nova 4, so far I'm using a variable to store the field and calling withoutTrashed() if needed.

        $categorySelect = BelongsTo::make('Category', 'category', 'App\Nova\Category')
            ->rules('required')
            // Default to Main category.
            ->withMeta([
                "belongsToId" => 2,
            ])
            ->relatableQueryUsing(function (NovaRequest $request, Builder $query) {
                $query->whereIsPublished(true)->orderBy('sort_order', 'asc');
            })->dontReorderAssociatables()
            ->sortable()
            ->filterable()
            ->help("If you feel a new category is required please contact our support.");

        if (!$user->is_admin) {
            $categorySelect->withoutTrashed();
        }

If the withoutTrashed had a parameter defaulting to true might be nicer.

    /**
     * hides the "With Trashed" option.
     * bool|null $value
     * @return $this
     */
    public function withoutTrashed($value=true)
    {
        $this->displaysWithTrashed = $value;

        return $this;
    }