F
Filament12mo ago
Sidem

table count status

I want to create a table that counts the number of lines by status of the 'Demande' database. so I do this:
public function table(Table $table): Table
{
return $table
->query(DemandeResource::getEloquentQuery())
->modifyQueryUsing(fn (Builder $query) =>
$query
->where('demandeur_id', auth()->user()->id))
->columns([
TextColumn::make('statut')
->summarize(Sum::make('statut')),
))
->defaultGroup('statut')
->groupsOnly();
}
}
public function table(Table $table): Table
{
return $table
->query(DemandeResource::getEloquentQuery())
->modifyQueryUsing(fn (Builder $query) =>
$query
->where('demandeur_id', auth()->user()->id))
->columns([
TextColumn::make('statut')
->summarize(Sum::make('statut')),
))
->defaultGroup('statut')
->groupsOnly();
}
}
i you have any idees it will be nice 🙂 Thanks you !
28 Replies
Sidem
SidemOP12mo ago
For information, my result is this : but i have 586 lines in my database "Demande" so it's make no sense
No description
ChesterS
ChesterS12mo ago
Um, it's the sum of all the values? I think you want to count them, right? So you can try ->summarize(Count::make('statut')), instead Or you can do it using SQL (select count(1) ... group by statut) but that's a bit more involved
Sidem
SidemOP12mo ago
thanks you a lot it was this :
->summarize(Count::make('statut')),
->summarize(Count::make('statut')),
Sidem
SidemOP12mo ago
and you know how to customize the "Compteur" on the screen ?
No description
ChesterS
ChesterS12mo ago
I think you can do
Count::make('statut')->label('Your Label')
Count::make('statut')->label('Your Label')
Sidem
SidemOP12mo ago
You'r a monster thanks !! I have a last question : I want to remove the status number (like 11 = Create, 13 = End, ...) so i create my Enum for status. so in my page DemandeRessource.php, if i do this :
Tables\Columns\TextColumn::make('statut')
->searchable()
->sortable()
->formatStateUsing(fn (string $state): string => DemandeStatutEnum::getStatut($state)),
Tables\Columns\TextColumn::make('statut')
->searchable()
->sortable()
->formatStateUsing(fn (string $state): string => DemandeStatutEnum::getStatut($state)),
its works and my status label replace my status number. But in my page Widget\NumberDemandes.php, i do this and my dd() is not detected :
return $table
->query(DemandeResource::getEloquentQuery())
->defaultGroup('statut')
->groupsOnly()
->columns([
TextColumn::make('statut')
->formatStateUsing(fn (string $state): string => dd(DemandeStatutEnum::getStatut($state)))
->summarize(Count::make('statut')->label('Total')),
]);
return $table
->query(DemandeResource::getEloquentQuery())
->defaultGroup('statut')
->groupsOnly()
->columns([
TextColumn::make('statut')
->formatStateUsing(fn (string $state): string => dd(DemandeStatutEnum::getStatut($state)))
->summarize(Count::make('statut')->label('Total')),
]);
if i remove dd() and i put just
->formatStateUsing(fn (string $state): string => DemandeStatutEnum::getStatut($state))
->formatStateUsing(fn (string $state): string => DemandeStatutEnum::getStatut($state))
that didn"t works You know how to do it ?
ChesterS
ChesterS12mo ago
what is the value of $state? Also have a look at this page, it might help simplify the code https://filamentphp.com/docs/3.x/support/enums
ChesterS
ChesterS12mo ago
And what does the getStatut() method do?
Sidem
SidemOP12mo ago
getStatut() take status number (1 for example) and return the label ('Create' for example) $state value's is the status number
ChesterS
ChesterS12mo ago
Ok, if you follow the instructions in that page, you won't have to do any of this 🙂
Sidem
SidemOP12mo ago
But i can't use number on my bdd if i follow the instructions of the link ?
ChesterS
ChesterS12mo ago
You need to add the HasLabel interface add a getLabel (like the example) What do you mean? it doesn't have to be strings, it can be numbers can you share your enum?
Sidem
SidemOP12mo ago
namespace App\Enums;

enum DemandeStatutEnum:string
{
const STATUT_TEXTES = [
1 => "Créé",
2 => "A traiter",
3 => "Validé",
4 => "Rejeté",
5 => "En attente",
6 => "En cours",
7 => "Programmé",
8 => "Qualifié",
9 => "Réceptionné",
10 => "Arbitrage annulé",
11 => "Liquidé",
12 => "Reporté",
13 => "Terminé",
];

const CREE = 1;
const A_TRAITER = 2;
const VALIDE = 3;
const REJECTED = 4;
const PENDING = 5;
const EN_COURS = 6;
const PROGRAMME = 7;
const QUALIFIE = 8;
const RECEPTIONNE = 9;
const ARBITRE_ANNULE = 10;
const LIQUIDE = 11;
const REPORTE = 12;
const TERMINE = 13;

public static function getStatut($chiffre_statut)
{
return self::STATUT_TEXTES[$chiffre_statut] ?? null;
}

public static function getStatutId(string $statut)
{
foreach (self::STATUT_TEXTES as $id => $text) {
if ($text === $statut) {
return $id;
}
}

return null;
}
}
namespace App\Enums;

enum DemandeStatutEnum:string
{
const STATUT_TEXTES = [
1 => "Créé",
2 => "A traiter",
3 => "Validé",
4 => "Rejeté",
5 => "En attente",
6 => "En cours",
7 => "Programmé",
8 => "Qualifié",
9 => "Réceptionné",
10 => "Arbitrage annulé",
11 => "Liquidé",
12 => "Reporté",
13 => "Terminé",
];

const CREE = 1;
const A_TRAITER = 2;
const VALIDE = 3;
const REJECTED = 4;
const PENDING = 5;
const EN_COURS = 6;
const PROGRAMME = 7;
const QUALIFIE = 8;
const RECEPTIONNE = 9;
const ARBITRE_ANNULE = 10;
const LIQUIDE = 11;
const REPORTE = 12;
const TERMINE = 13;

public static function getStatut($chiffre_statut)
{
return self::STATUT_TEXTES[$chiffre_statut] ?? null;
}

public static function getStatutId(string $statut)
{
foreach (self::STATUT_TEXTES as $id => $text) {
if ($text === $statut) {
return $id;
}
}

return null;
}
}
ChesterS
ChesterS12mo ago
And don't foget this
When using an enum with an attribute on your Eloquent model, please ensure that it is cast correctly.
https://laravel.com/docs/10.x/eloquent-mutators#enum-casting
Sidem
SidemOP12mo ago
getStatut -> take int and return string getStatutId -> take string and return int
ChesterS
ChesterS12mo ago
Yeah this can be 'simplified'. Also, it's not a great idea to get an enum from a string since strings can be translated BTW your enum is wrong heh Gimme a sec
enum DemandeStatutEnum: int implements HasLabel
{
case CREE = 1;
case A_TRAITER = 2;
case VALIDE = 3;
case REJECTED = 4;
case PENDING = 5;
case EN_COURS = 6;
case PROGRAMME = 7;
case QUALIFIE = 8;
case RECEPTIONNE = 9;
case ARBITRE_ANNULE = 10;
case LIQUIDE = 11;
case REPORTE = 12;
case TERMINE = 13;

public function getLabel(): string
{
return match ($this) {
self::CREE => __('Créé'),
self::A_TRAITER => __('A traiter'),
self::VALIDE => __('Validé'),
self::REJECTED => __('Rejeté'),
self::PENDING => __('En attente'),
self::EN_COURS => __('En cours'),
self::PROGRAMME => __('Programmé'),
self::QUALIFIE => __('Qualifié'),
self::RECEPTIONNE => __('Réceptionné'),
self::ARBITRE_ANNULE => __('Arbitrage annulé'),
self::LIQUIDE => __('Liquidé'),
self::REPORTE => __('Reporté'),
self::TERMINE => __('Terminé'),
};
}
}
enum DemandeStatutEnum: int implements HasLabel
{
case CREE = 1;
case A_TRAITER = 2;
case VALIDE = 3;
case REJECTED = 4;
case PENDING = 5;
case EN_COURS = 6;
case PROGRAMME = 7;
case QUALIFIE = 8;
case RECEPTIONNE = 9;
case ARBITRE_ANNULE = 10;
case LIQUIDE = 11;
case REPORTE = 12;
case TERMINE = 13;

public function getLabel(): string
{
return match ($this) {
self::CREE => __('Créé'),
self::A_TRAITER => __('A traiter'),
self::VALIDE => __('Validé'),
self::REJECTED => __('Rejeté'),
self::PENDING => __('En attente'),
self::EN_COURS => __('En cours'),
self::PROGRAMME => __('Programmé'),
self::QUALIFIE => __('Qualifié'),
self::RECEPTIONNE => __('Réceptionné'),
self::ARBITRE_ANNULE => __('Arbitrage annulé'),
self::LIQUIDE => __('Liquidé'),
self::REPORTE => __('Reporté'),
self::TERMINE => __('Terminé'),
};
}
}
I would suggest you read a bit about enums. You don't need to explicitly give them values unless you want/need to.
Sidem
SidemOP12mo ago
ok I put that in my code but then how do I display the status correctly? because according to the link there is the ->options() method, but it does not apply to the TextInput
ChesterS
ChesterS12mo ago
Why do you want options on a TextInput? Use a Select ...?
Sidem
SidemOP12mo ago
I can't because Select don't know
->summarize(Count::make('status'))
->summarize(Count::make('status'))
"Method Filament\Forms\Components\Select::summarize does not exist."
ChesterS
ChesterS12mo ago
I don't understand what you're trying to do. You want to be able to change the status from the summary table?
Sidem
SidemOP12mo ago
No it's the same problem : I want to change the numbers to the statuses that correspond to them
No description
Sidem
SidemOP12mo ago
So 13 = finished, ....
ChesterS
ChesterS12mo ago
Ok, this is a table. Nothing to do with TextInput. You need TextColumn. Follow the instructions here https://filamentphp.com/docs/3.x/support/enums and it should work as expected
Sidem
SidemOP12mo ago
I follow the instructions but it didn't work, I have the same result and the same problem : my StatutEnum.php :
enum StatutEnum: int implements HasLabel
{
case CREE = 1;
case A_TRAITER = 2;


public function getLabel(): string
{
return match ($this) {
self::CREE => __('Créé'),
self::A_TRAITER => __('A traiter'),
};
}
}
enum StatutEnum: int implements HasLabel
{
case CREE = 1;
case A_TRAITER = 2;


public function getLabel(): string
{
return match ($this) {
self::CREE => __('Créé'),
self::A_TRAITER => __('A traiter'),
};
}
}
my Filament\Widgets\NombreDeDemande.php :
class NombreDeDemande extends BaseWidget
{
public function table(Table $table): Table
{
return $table
->query(DemandeResource::getEloquentQuery())
->defaultGroup('status')
->groupsOnly()
->columns([
TextColumn::make('status')
->summarize(Count::make('status')->label('Total')),
]);
}
}
class NombreDeDemande extends BaseWidget
{
public function table(Table $table): Table
{
return $table
->query(DemandeResource::getEloquentQuery())
->defaultGroup('status')
->groupsOnly()
->columns([
TextColumn::make('status')
->summarize(Count::make('status')->label('Total')),
]);
}
}
I think it's because nowhere do I say that the status of the NumberRequests file is linked to the status of the StatusEnum file have you any idee ?
ChesterS
ChesterS12mo ago
Do you cave a cast in your DemandeResource model?
$casts = [
'statut' => StatutEnum::class,
];
$casts = [
'statut' => StatutEnum::class,
];
Sidem
SidemOP12mo ago
Man thank you a lot for all 🙏 Have a good day !
ChesterS
ChesterS12mo ago
Haha did it work or are you just not interested any more? 😂
Sidem
SidemOP12mo ago
It's work !
No description
Want results from more Discord servers?
Add your server