taqi
taqi
FFilament
Created by taqi on 2/4/2024 in #❓┊help
Solutions to Repetitive Code
<?php

namespace App\Helpers;

use Filament\Infolists\Components\TextEntry;

class TextEntryPremium
{
public $hasAccess = false;

public static function make(string $column): TextEntry
{
return TextEntry::make($column)
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : '');
}
}
<?php

namespace App\Helpers;

use Filament\Infolists\Components\TextEntry;

class TextEntryPremium
{
public $hasAccess = false;

public static function make(string $column): TextEntry
{
return TextEntry::make($column)
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : '');
}
}
use App\Helpers\TextEntryPremium;

public function profileInfolist(Infolist $infolist): Infolist
{
return $infolist
->record($this->profile)
->schema([
TextEntry::make('name'),
TextEntryPremium::make('email'),
]);
}
use App\Helpers\TextEntryPremium;

public function profileInfolist(Infolist $infolist): Infolist
{
return $infolist
->record($this->profile)
->schema([
TextEntry::make('name'),
TextEntryPremium::make('email'),
]);
}
@Tieme This is the final solution I came up with
8 replies
FFilament
Created by taqi on 2/4/2024 in #❓┊help
Solutions to Repetitive Code
I agree. I may need that in near future. I will go with the helper class approach then.
8 replies
FFilament
Created by taqi on 2/4/2024 in #❓┊help
Solutions to Repetitive Code
BINGO!! IT WORKS!!!! Now I have to write this for my solution:
// customer function
public function applyPremiumDataRestrictions(string $column): TextEntry
{
return TextEntry::make($column)
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : '');
}

// inside schema
$this->applyPremiumDataRestrictions('name')->label(__('Name)),
// customer function
public function applyPremiumDataRestrictions(string $column): TextEntry
{
return TextEntry::make($column)
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : '');
}

// inside schema
$this->applyPremiumDataRestrictions('name')->label(__('Name)),
Instead of a helper class, I have created another function inside the same class. Thanks a lot my friend @Tieme 😊
8 replies