How to run a query based on fields and show output in another field
This is my form :
return $form
->schema([
Group::make([
Toggle::make('has_verified_email')->reactive(),
Toggle::make('has_verified_phone')->reactive(),
Toggle::make('new_users_only')->reactive()
->afterStateUpdated(fn (Closure $set, $get, $state) => $get('new_users_only') ? $set('old_users_only', !$state) : ''),
Toggle::make('old_users_only')->reactive()
->afterStateUpdated(fn (Closure $set, $get, $state) => $get('old_users_only') ? $set('new_users_only', !$state) : ''),
Forms\Components\TextInput::make('test')->afterStateUpdated(
function (Closure $get) {
$count = User::when($get('has_verified_email'), function ($q) {
$q->whereNotNull('email_verified_at');
})->when($get('has_verified_phone'), function ($q) {
$q->whereNotNull('has_verified_phone');
})->when($get('new_users_only'), function ($q) {
$q->whereDate('created_at', '>=', now()->subMonth());
})->when($get('old_users_only'), function ($q) {
$q->whereDate('created_at', '<=', now()->subMonth());
})->count();
return "$count users selected!";
}
)
->columnSpanFull()->disabled()->disableLabel()
])->columnSpan(12)->columns(4),
I want to fill up the field 'test' with the count of users based on the other toggles set. I could set it using default but it works in first load only. I want to update the 'test' every time a toggle is changed.
Is there a way to achieve this? Thank You
2 Replies
Your text input ('test') not need
->afterStateUpdated()
try change this one with placeholder
and you can see the effect..
I am just showing how you can see changes, but for actual value you have to find a way cause I'm not familiar about input type changing@.valpuia Thank you so much. It worked!