Updated the Receipts model and Resource and now have the icons working but no comment count in the column
Icon and Comment count not updating
I have Receipt and Comment model. When a new receipt is created the “has_notes” column in the Receipt model should display an icon “heroicon-o-document-arrow-up” and comment count of 0. Once the receipt record is created a user can then go in and add a note which is saved to the Comment model and the Receipt model should display an icon “heroicon-o-document” and comment should increment up.
In Filament 3 / Laravel 11 I have created the Receipt Resource and a CommentsRelationManager but cannot get the icon to change or the comment count to increment once a note is present in the record? Can anyone suggest a solution to this?
The CommentsRelationManager
// app/Filament/Resources/ReceiptResource/RelationManagers/CommentsRelationManager.php
namespace App\Filament\Resources\ReceiptResource\RelationManagers;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Model;
class CommentsRelationManager extends RelationManager
{
protected static string $relationship = 'comments';
protected static ?string $recordTitleAttribute = 'comment';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Textarea::make('comment')
->required()
->label('Comment'),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('comment')->label('Comment'),
TextColumn::make('created_at')->label('Created At')->dateTime(),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
]);
}
The ReceiptsResource
// app/Filament/Resources/ReceiptResource.php
namespace App\Filament\Resources;
use App\Filament\Resources\ReceiptResource\Pages;
use App\Filament\Resources\ReceiptResource\RelationManagers\CommentsRelationManager;
use App\Models\Receipt;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class ReceiptResource extends Resource
{
protected static ?string $model = Receipt::class;
public static function form(Form $form): Form
{
return $form
->schema([
// Other existing sections...
Forms\Components\Section::make('Comments & Attachments Available')
->columns(2)
->collapsible()
->collapsed()
->schema([
TextInput::make('has_notes')
->label('Comments')
->default('heroicon-o-document-arrow-up')
->hidden(fn ($record) => $record === null)
->hiddenOn(['create', 'edit']),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->heading('Receipt')
->description('View All Receipt Records')
->persistFiltersInSession()
->filtersTriggerAction(function ($action) {
return $action->button()->label('Filters');
})
->columns([
IconColumn::make('has_notes')
->boolean()
->trueIcon('heroicon-o-document-text')
->falseIcon('heroicon-o-document-arrow-up')
->label('Notes')
->extraAttributes(function ($record) {
return [
'data-count' => $record->comment_count,
];
}),
])
public static function getRelations(): array
{
return [
CommentsRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListReceipts::route('/'),
'create' => Pages\CreateReceipt::route('/create'),
'edit' => Pages\EditReceipt::route('/{record}/edit'),
'view' => Pages\ViewReceipt::route('/{record}'),
];
}
}
Comment Model
// app/Models/Comment.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use HasFactory;
protected $fillable = [
'receipt_id',
'comment',
];
public function receipt()
{
return $this->belongsTo(Receipt::class);
}
}
Receipts Model
// app/Models/Receipt.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Receipt extends Model
{
use HasFactory;
protected $fillable = [
'has_notes',
];
public function comments()
{
return $this->hasMany(Comment::class);
}
// Accessor to get the comment count
public function getCommentCountAttribute(): int
{
return $this->comments()->count();
}
// Accessor to check if there are notes
public function getHasNotesAttribute(): bool
{
return $this->comments()->exists();
}
}
Please or to participate in this conversation.