syed_eer
How to Use ->relationship() with $get() in $form?
I'm working on a Filament form where gst_customer_name should be saved in the guest_customers table when the user enters a name that doesn't match an existing customer.
~
TextInput::make('customer_name')
->afterStateUpdated(function ($state, callable $set) { $customer = \App\Models\Customer::where('customer_name', 'like', '%' . $state . '%')->first(); if ($customer) { $set('customer_id', $customer->id); $set('gst_customer_name', null); $set('gst_tmp_name', null); } else { $set('customer_id', null); $set('guestCustomer.gst_customer_name', $state); $set('gst_tmp_name', $state); } }) Hidden::make('customer_id'), TextInput::make('gst_tmp_name'), Fieldset::make()->relationship('guestCustomer') ->schema([ TextInput::make('gst_customer_name') ->default(function($state, $set, $get){ // dd($state); $set('gst_customer_name', $get('../../gst_tmp_name')); $set('gst_customer_name', $state); }), TextInput::make('guestCustomer.gst_customer_name') ->default(function($state, $set, $get){ // dd($state); $set('gst_customer_name', $get('gst_tmp_name')); $set('gst_customer_name', $state); }), ]), ~
->afterStateUpdated(function ($state, callable $set) { $customer = \App\Models\Customer::where('customer_name', 'like', '%' . $state . '%')->first(); if ($customer) { $set('customer_id', $customer->id); $set('gst_customer_name', null); $set('gst_tmp_name', null); } else { $set('customer_id', null); $set('guestCustomer.gst_customer_name', $state); $set('gst_tmp_name', $state); } }) Hidden::make('customer_id'), TextInput::make('gst_tmp_name'), Fieldset::make()->relationship('guestCustomer') ->schema([ TextInput::make('gst_customer_name') ->default(function($state, $set, $get){ // dd($state); $set('gst_customer_name', $get('../../gst_tmp_name')); $set('gst_customer_name', $state); }), TextInput::make('guestCustomer.gst_customer_name') ->default(function($state, $set, $get){ // dd($state); $set('gst_customer_name', $get('gst_tmp_name')); $set('gst_customer_name', $state); }), ]), ~
6 replies
Select options are not loading along with afterStateHydrated(). whats wrong with my code?
Select options are not loading with model_ids when I'm using afterStateHydrated() to trim these model_ids and format correct JSON. These model_ids are array of foreign key and I want it to be saved as JSON like this
"[253,256,255]".
~
Forms\Components\Select::make('model_id')
->label('Vehicle Models')
->options(function ($get) {
$makeId = $get('make_id');
$models = \App\Models\VehicleModel::where('make_id', $makeId)
->pluck('model_name', 'id')
->toArray();
return $models; }) ->multiple()
->preload() ->afterStateHydrated(function ($state, callable $set) { try {
if (is_string($state)) { $state = stripslashes($state); $state = json_decode($state, true); }
if (!is_array($state)) { $state = []; }
if (count($state) === 1 && is_string($state[0])) {
$state = explode(',', str_replace('"', '', $state[0])); } $cleanedState = array_map(function ($item) { return preg_replace('/[^0-9]/', '', trim($item)); }, $state); $cleanedState = array_filter($cleanedState);
$mdlIds = json_encode(array_values($cleanedState)); $set('model_id', $mdlIds); } catch (\Exception $e) {
report($e); $set('model_id', '[]'); } }), ~
return $models; }) ->multiple()
->preload() ->afterStateHydrated(function ($state, callable $set) { try {
if (is_string($state)) { $state = stripslashes($state); $state = json_decode($state, true); }
if (!is_array($state)) { $state = []; }
if (count($state) === 1 && is_string($state[0])) {
$state = explode(',', str_replace('"', '', $state[0])); } $cleanedState = array_map(function ($item) { return preg_replace('/[^0-9]/', '', trim($item)); }, $state); $cleanedState = array_filter($cleanedState);
$mdlIds = json_encode(array_values($cleanedState)); $set('model_id', $mdlIds); } catch (\Exception $e) {
report($e); $set('model_id', '[]'); } }), ~
2 replies
Filament Repeater Not Passing Foreign Key (make_id) - SQL Error 1364
I'm using a Filament Repeater to add vehicle compatibility records in my Laravel app. The make_id field is a foreign key, but when saving, I get this error:
SQLSTATE[HY000]: General error: 1364 Field 'make_id' doesn't have a default value (Connection: mysql, SQL: insert into
part_compatibility
(part_id
, updated_at
, created_at
) values (29, 2025-02-04 08:42:29, 2025-02-04 08:42:29))
CompatibilitiesRelationManager file attached
Part Model:
class Part extends Model
{
protected $fillable = [
'supplier_id',
'part_name',
'part_number',
'brand',
'description',
'category',
'unit_of_measurement',
'stock_level',
'quantity_per_unit',
'purchase_price',
'selling_price',
'remarks',
];
public function supplier()
{
return $this->belongsTo(Supplier::class, 'supplier_id');
}
public function compatibilities()
{
return $this->hasMany(PartCompatibility::class);
}
}
PartCompatibility Model:
class PartCompatibility extends Model
{
protected $table = 'part_compatibility';
protected $fillable = [
'part_id',
'make_id',
'model_id',
'years',
];
protected $casts = [
'model_id' => 'array', // Cast model_id to an array
'years' => 'array', // Cast years to an array
];
public function part()
{
return $this->belongsTo(Part::class, 'part_id');
}
public function make()
{
return $this->belongsTo(VehicleMake::class, 'make_id');
}
public function model()
{
return $this->belongsTo(VehicleModel::class, 'model_id');
}
}
5 replies
I'm trying to show repeaters conditionally using checkbox, it works but not as required?
In my filament v3 project, I'm trying to show repeaters conditionally using checkbox which is for the available customers. something like
Section::make('Item Pricing / Linking')->label('Item Pricing / Linking')
->schema([
Checkbox::make('show_all')
->label('Show All')
->live()->afterStateUpdated(function ($state, callable $set) {
if ($state) {
$customers = Customer::all()->toArray();
$set('prices', collect($customers)
->map(fn ($customer) => [
'customer_id' => $customer['id'],
'customer_barcode' => '',
'customer_ref' => '',
'price' => '',
'is_linked' => true, ])->toArray());
} else {
$set('prices', []);
}
}),
Repeater::make('prices')
->relationship('prices')
->schema([
Select::make('customer_id')
->relationship('customer', 'name')
->label('Customer Name'),
TextInput::make('customer_barcode')
->label('Customer Barcode')
->required(),
TextInput::make('customer_ref')
->label('Customer Reference')
->required(),
TextInput::make('price')
->numeric()
->label('Price')
->required(),
Toggle::make('is_linked')
->label('Linked')
->default(true),
])->columns(2)
->live()
->collapsed(false)
->createItemButtonLabel('Add Customer'),
])->columns(1),
it works but not as required. On page load it show blank repeater and when checkbox is checked it shows perfectly as needed. However, when I unchecked this checkbox it hides the whole repeater and I dont want it like that, I want it to be back in the on page load state with one black repeater.
is it possible to achieve? Please help me!2 replies
Can I make getTab() in 2 lines?
Hello all,
In my Filament project, I have 6 to 8 different types of payments represented by filter tabs in the getTabs() function. Currently, these tabs are displayed in a single line, making the interface look congested. Is there a way to arrange these tabs in multiple lines or columns for a cleaner layout? Thanks in advance for your time and help.
@Dennis Koch
11 replies
Filament v3 has active tab filters, can I have the same in v2 also?
I was just checking v3 demo and found this feature of an active tab for the status column very interesting. Can I have this feature in v2 also? Thank you for the reply in advance.
4 replies
Text or TextInput fiedl can be dynamically populated?
In my filament project, I've created the student form which has details about fee_plan and entered some
data as well. Now, I'm preparing a form for fee payment, here I can select students from dropdown field
and when I select a student I want fee_plan to be automatically populated next to a student name as a
text or uneditable textinput field. Is this possible to achieve? Thanks for your help in advance.
This is the code
This is the code
Select::make('fee_plan_id')//states
->label('Student Fee Plan')
->options(function (Get $get){
$students = Student::find($get('student_id'));
if(!$students){
return FeePlan::all()->pluck('name', 'id');
}
dd($students->fee_plan->name);
return $students->fee_plan->name;
}),
46 replies