F
Filamentβ€’15mo ago
Samer

remove the single DeleteAction from specific records

remove the single DeleteAction from specific rows, for example, users with is_admin = 1 so admin users can't be deleted. tried to use this ->before(function (DeleteAction $action) { if (! $this->record->is_admin = 1) { but get that error about ' $this ' i'm very new πŸ˜„
17 Replies
Dan Harrin
Dan Harrinβ€’15mo ago
->hidden(fn (User $record): bool => $user->is_admin)
Samer
Samerβ€’15mo ago
thanks @Dan Harrin I've tried to add it as bellow ->actions([ Tables\Actions\ViewAction::make(), Tables\Actions\EditAction::make(), Tables\Actions\DeleteAction::make()->hidden(fn (User $record): bool => $user->is_admin) ]) but when i load the page I get Attempt to read property "is_admin" on null
Dan Harrin
Dan Harrinβ€’15mo ago
weird, can you share the full code because im 99% sure the code you just pasted is not the code you are actually using since if it was the code you were using, the error would be different. since User is not a nullable type
Samer
Samerβ€’15mo ago
Filament
Filamentβ€’15mo ago
We need more information to help you debug your problem. Please click on the top left 'SHARE' button of the error page you're seeing and share the link with us.
Samer
Samerβ€’15mo ago
Flare
Attempt to read property "is_admin" on null - The error occurred at http://baseapp.test/tenant_manager/users
Dan Harrin
Dan Harrinβ€’15mo ago
sorry ive just realised my stupid mistake ->hidden(fn (User $record): bool => $record->is_admin) πŸ€¦β€β™‚οΈ
Samer
Samerβ€’15mo ago
actually i though about it and got an error also , https://flareapp.io/share/qm1dwOp5
Flare
Typed property App\Models\User::$is_admin must not be accessed before initialization - The error occurred at http://baseapp.test/tenant_manager/users
Dan Harrin
Dan Harrinβ€’15mo ago
is is_admin not a column in your db table? why is it a property
Samer
Samerβ€’15mo ago
its a column
Dan Harrin
Dan Harrinβ€’15mo ago
can you send your model
Samer
Samerβ€’15mo ago
ok $table->boolean('is_admin')->after('name')->default(0); <?php namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Filament\Models\Contracts\FilamentUser; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; use Spatie\Permission\Traits\HasRoles; use Illuminate\Database\Eloquent\Concerns\HasUuids; class User extends Authenticatable implements FilamentUser { use HasApiTokens, HasFactory, Notifiable, HasRoles, SoftDeletes, HasUuids; / * @var int|mixed */ public mixed $is_admin; / * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'is_admin', 'email', 'password', ]; / * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; / * The attributes that should be cast. * * @var array<string, string> */ protected $casts = [ 'email_verified_at' => 'datetime', ];
public function canAccessFilament(): bool { return str_ends_with($this->email, '@admin.com'); } }
Dan Harrin
Dan Harrinβ€’15mo ago
why is it a property lol you dont need it if its a column?
Samer
Samerβ€’15mo ago
sorry im' not geting it 😦
Dan Harrin
Dan Harrinβ€’15mo ago
you dont need public mixed $is_admin; its useless in eloquent and breaks it
Samer
Samerβ€’15mo ago
oh got it Thanks for your time , I'm not a developer i'm just someone how google things and try to make them work πŸ˜›
Dan Harrin
Dan Harrinβ€’15mo ago
haha ok