Closed laraning closed 5 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
Some solution?
+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;
}
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();
}
Works like a charm, thanks!
Closing as the solution was posed above.
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
You could potentially check the current URI using the request()
helper, but it'd be a bit of a workaround.
Is it not possible to chain a ->hideWithTrashed() like we do with ->hideFromIndex() ?
hope add disableTrashed(true|false) just like R64\NovaFields\BelongsTo
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(),
@briavers but not on BelongsToMany ... I hope they add also in BelongsToMany too.
@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>
@tpetry ok thanks. I'll use it till they add the functionality for BelongsToMany.
Any news?
Hi~ The BelongsToMany field has not withoutTrashed method, how to filter the pivot that use SoftDeletes? thx~ nova version: v2.9.3
+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.
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.
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?
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.
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;
}
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:
Is there a way to do it?
Thank you for the help!