Delete method inside model policy not working after editing

I tried to disable/hide the delete function when a customer has any comments. It works normally. However, after updating the customer, the delete button appears, which shouldn't be there. Example Code:
// app\Policies\Shop\CustomerPolicy.php
class CustomerPolicy
{

public function delete(User $user, Customer $customer): bool
{
if ($customer->comments_exists) {
return false;
}

return true;
}

}

// app\Filament\Resources\Shop\Customers\CustomerResource.php
class CustomerResource extends Resource
{

// ...

/** @return Builder<Customer> */
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with('addresses')->withoutGlobalScope(SoftDeletingScope::class)->withExists('comments');
}
}
// app\Policies\Shop\CustomerPolicy.php
class CustomerPolicy
{

public function delete(User $user, Customer $customer): bool
{
if ($customer->comments_exists) {
return false;
}

return true;
}

}

// app\Filament\Resources\Shop\Customers\CustomerResource.php
class CustomerResource extends Resource
{

// ...

/** @return Builder<Customer> */
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->with('addresses')->withoutGlobalScope(SoftDeletingScope::class)->withExists('comments');
}
}
Solution:
I found a workaround for this, inside the delete method policy I added this code: ```php public function delete(User $user, Customer $customer): bool { if ($customer->comments_exists ?? $customer->comments()->exists()) {...
Jump to solution
2 Replies
Sharpshooter
SharpshooterOP2mo ago
here's the video that showing the bug
Solution
Sharpshooter
Sharpshooter2mo ago
I found a workaround for this, inside the delete method policy I added this code:
public function delete(User $user, Customer $customer): bool
{
if ($customer->comments_exists ?? $customer->comments()->exists()) {
return false;
}

return true;
}
public function delete(User $user, Customer $customer): bool
{
if ($customer->comments_exists ?? $customer->comments()->exists()) {
return false;
}

return true;
}
Does anyone know why this is happening?

Did you find this page helpful?