F
Filament16mo ago
Ninja++

syntax error, unexpected identifier "Stat", expecting "function" or "const"

I am trying to make a widget on my materials resource page that shows the total number of materials in the table. I am unsure why it is not expecting
Stat
Stat
<?php

namespace App\Filament\Resources\MaterialsResource\Widgets;

use Filament\Widgets\Widget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Database\Eloquent\Model;


class MaterialsWidget extends Widget
{
use ExposesTableToWidgets;
use InteractsWithPageTable;


protected function getTablePage(): string
{
return ListMaterials::class;
}

Stat::make('Total Materials', $this->getPageTableRecords()->count()),

protected static string $view = 'filament.resources.materials-resource.widgets.materials-widget';
}
<?php

namespace App\Filament\Resources\MaterialsResource\Widgets;

use Filament\Widgets\Widget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Database\Eloquent\Model;


class MaterialsWidget extends Widget
{
use ExposesTableToWidgets;
use InteractsWithPageTable;


protected function getTablePage(): string
{
return ListMaterials::class;
}

Stat::make('Total Materials', $this->getPageTableRecords()->count()),

protected static string $view = 'filament.resources.materials-resource.widgets.materials-widget';
}
7 Replies
Dennis Koch
Dennis Koch16mo ago
You put it outside of a method So the statement Stat:: is in a place where no statements are allowed.
Dennis Koch
Dennis Koch16mo ago
Have another look at the Docs. It belongs inside a getStats() method. https://filamentphp.com/docs/3.x/widgets/stats-overview
Ninja++
Ninja++OP16mo ago
Thank you for that it has worked, however my widget is still not showing up on the resoruce page. I think i have registed it corrently and put it in the geader section. Here is the code from the resource.
public static function getPages(): array
{
return [
'index' => Pages\ListMaterials::route('/'),
'create' => Pages\CreateMaterials::route('/create'),
'edit' => Pages\EditMaterials::route('/{record}/edit'),
'view' => Pages\ViewMaterials::route('/{record}/view')
];
}


public static function getWidgets(): array
{
return [
MaterialsResource\Widgets\MaterialsWidget::class,
];
}

protected function getHeaderWidgets(): array
{

return[

MaterialsResource\Widgets\MaterialsWidget::class,

];
}
public static function getPages(): array
{
return [
'index' => Pages\ListMaterials::route('/'),
'create' => Pages\CreateMaterials::route('/create'),
'edit' => Pages\EditMaterials::route('/{record}/edit'),
'view' => Pages\ViewMaterials::route('/{record}/view')
];
}


public static function getWidgets(): array
{
return [
MaterialsResource\Widgets\MaterialsWidget::class,
];
}

protected function getHeaderWidgets(): array
{

return[

MaterialsResource\Widgets\MaterialsWidget::class,

];
}
Dennis Koch
Dennis Koch16mo ago
getHeaderWidgets() belongs on the Page class. Like EditMaterials or ListMaterials
Ninja++
Ninja++OP16mo ago
This is the page class and the error
View [filament.resources.materials-resource.widgets.materials-widget] not found.
View [filament.resources.materials-resource.widgets.materials-widget] not found.
<?php

namespace App\Filament\Resources\MaterialsResource\Pages;

use App\Filament\Resources\MaterialsResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewMaterials extends ViewRecord
{
protected static string $resource = MaterialsResource::class;

protected function getHeaderWidgets(): array
{

return[

MaterialsResource\Widgets\MaterialsWidget::class,

];
}

public static function getWidgets(): array
{
return [
MaterialsResource\Widgets\MaterialsWidget::class,
];
}

}
<?php

namespace App\Filament\Resources\MaterialsResource\Pages;

use App\Filament\Resources\MaterialsResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewMaterials extends ViewRecord
{
protected static string $resource = MaterialsResource::class;

protected function getHeaderWidgets(): array
{

return[

MaterialsResource\Widgets\MaterialsWidget::class,

];
}

public static function getWidgets(): array
{
return [
MaterialsResource\Widgets\MaterialsWidget::class,
];
}

}
Dennis Koch
Dennis Koch16mo ago
Did you generate the widget via CLI? Search for the view file. The error is pretty self explanatory I didn’t say you should move getWidgets() method. Only getHeaderWidgets()
Ninja++
Ninja++OP16mo ago
Yes I generated via the CLI I read the error see all the parts and get them individually but I'm struggling to parse that into what I need to do
Want results from more Discord servers?
Add your server