Static property state does not persist

I don't know if this is a general PHP thing or has got to do with the way Filament works or I'm doing something wrong but somehow the state doesn't persist. I'm setting a static property in the getSearchResultsUsing() function and afterwards retrieve that property in the afterStateUpdated() function but it errors with the message "must not be accessed before initialization".
Forms\Components\Select::make('title')
->required()
->searchable()
->reactive()
->getSearchResultsUsing(function (string $search) {
$tmdb = new TMDBConnector;
$request = new SearchMovieRequest($search);

$response = $tmdb->send($request);

$body = $response->body();
$decodedBody = $response->json();

$results = collect($decodedBody['results']);

static::$searchResults = $results;

return $results->pluck('title', 'id');
})
->afterStateUpdated(function (Closure $set, $state) {
dd(static::$searchResults);
})
Forms\Components\Select::make('title')
->required()
->searchable()
->reactive()
->getSearchResultsUsing(function (string $search) {
$tmdb = new TMDBConnector;
$request = new SearchMovieRequest($search);

$response = $tmdb->send($request);

$body = $response->body();
$decodedBody = $response->json();

$results = collect($decodedBody['results']);

static::$searchResults = $results;

return $results->pluck('title', 'id');
})
->afterStateUpdated(function (Closure $set, $state) {
dd(static::$searchResults);
})
What is happening here? What can I do to save data between the functions or am I doing something completely wrong conceptually. Thanks in advance!
4 Replies
Dennis Koch
Dennis Koch2y ago
It's a PHP thing. Those are two separate requests. One for getting the results and the next for updating the result
Quadrubo
QuadruboOP2y ago
Okay and how would I be able to store this data between the requests? Wanted to avoid making 2 requests to the api for data that is already present in the first request.
Dennis Koch
Dennis Koch2y ago
You could use a Cache. Depending on how often the data changes You could also just cache it for some seconds
Quadrubo
QuadruboOP2y ago
okay thank you :)

Did you find this page helpful?