This commit is contained in:
Bozhidar Slaveykov 2024-04-04 16:31:37 +03:00
parent 1241f19334
commit 64519f8c77
3 changed files with 75 additions and 5 deletions

View file

@ -33,11 +33,6 @@ class Customer extends Model
});
static::deleting(function ($model) {
$findWebistes = Website::where('customer_id', $model->id)->count();
if ($findWebistes > 0) {
throw new ('Customer has websites, please delete them first.');
return false;
}
event(new ModelCustomerDeleting($model));
});

View file

@ -0,0 +1,71 @@
<?php
namespace App\Policies;
use App\Models\Customer;
use App\Models\User;
use App\Models\Website;
use Illuminate\Auth\Access\Response;
class CustomerPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Customer $customer): bool
{
//
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Customer $customer): bool
{
//
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Customer $customer): bool
{
$findWebsites = Website::where('customer_id', $customer->id)->count();
if ($findWebsites > 0) {
return Response::deny('Customer has websites, please delete them first.');
}
return Response::allow();
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Customer $customer): bool
{
//
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Customer $customer): bool
{
//
}
}

View file

@ -10,8 +10,10 @@ use App\Listeners\ModelCustomerCreatedListener;
use App\Listeners\ModelCustomerDeletingListener;
use App\Listeners\ModelWebsiteCreatedListener;
use App\Listeners\ModelWebsiteDeletingListener;
use App\Policies\CustomerPolicy;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@ -30,6 +32,8 @@ class AppServiceProvider extends ServiceProvider
public function boot(): void
{
Gate::define('delete-customer', [CustomerPolicy::class, 'delete']);
Filament::serving(function () {
// Using Vite
Filament::registerViteTheme('resources/css/app.css');