It seems like you're trying to set up a dynamic form where the state_id is passed to the city creation form in Filament. To achieve this, you need to ensure that the state_id is available when creating a new city. Here's how you can modify your city_id field to pass the state_id to the city creation form:
Select::make('city_id')
->options(fn(Get $get): Collection => City::query()
->where('state_id', $get('state_id'))
->pluck('name', 'id'))
->searchable()
->preload()
->label('City')
->createOptionForm([
TextInput::make('name')->label('City')->required(),
Hidden::make('state_id')->default(fn(Get $get): int => $get('state_id')),
]),
In this code, I've replaced TextInput with Hidden for the state_id field in the createOptionForm. This is because you don't want the user to manually input the state_id; instead, it should be automatically passed from the selected state in the form.
Regarding displaying the value of a given variable in Filament, you would typically use the ->displayUsing() method on a field to format or display the value. However, if you're looking to display a variable directly in a view or a component, you would do it similarly to Blade syntax, but it depends on the context and the specific Filament component you're using.
For the autocomplete functionality without a modal, you can use the Select field with the ->searchable() method, which you already have in your code. This allows users to type and search for existing cities. If you want to allow users to add a new city if it doesn't exist in the search, you would need to handle this logic in your controller or form submission handling by checking if the city exists and creating it if not.
Here's a rough example of how you might handle the submission:
public function store(Request $request)
{
$cityName = $request->input('city_name');
$stateId = $request->input('state_id');
$city = City::firstOrCreate(
['name' => $cityName, 'state_id' => $stateId],
['other_attributes' => 'values'] // Add other necessary attributes for the City model
);
// Now you have your city, either retrieved or created, and you can proceed with the rest of your logic.
}
This code assumes you have a city_name input in your form and that you're handling the form submission in a store method. Adjust the attribute names and logic to fit your actual application.