F
Filament16mo ago
Pasteko

Is there any other way to write this?

Forms\Components\Select::make('year_id')
->options(Year::all()->pluck('year', 'id')->toArray())
->afterStateHydrated(function ($set, $state, $record) {
if (isset($record->product_id)) {
$set('year_id', Product::with('year')->where('id', $record->product_id)->value('year_id'));
}
})
->label('Year')
->reactive()
->required(),
Forms\Components\Select::make('year_id')
->options(Year::all()->pluck('year', 'id')->toArray())
->afterStateHydrated(function ($set, $state, $record) {
if (isset($record->product_id)) {
$set('year_id', Product::with('year')->where('id', $record->product_id)->value('year_id'));
}
})
->label('Year')
->reactive()
->required(),
`
21 Replies
Pasteko
Pasteko16mo ago
The purpose of this is to set the selected value in a select from a relation, is there another way to manage the create and edit without the isset condition?
Andrew Wallo
Andrew Wallo16mo ago
->afterStateHydrated(static function ($component, $state) use ($product_id) { $selected = array_intersect($product_id, $state); $component->state(array_keys($selected)); }), Maybe this will work im not sure
wyChoong
wyChoong16mo ago
If your model have relation, you probably can remove the afterStateHydrate And use Select::make(‘product.year_id’)
Pasteko
Pasteko16mo ago
Are you sure this works? For me it's not displaying anything.
toeknee
toeknee16mo ago
then your relationship isn't working Doing the Select from the relationship should return the year_id if you have year_id on your product relationship.
Pasteko
Pasteko16mo ago
Table : years - id - year Table : products - id - name - description - year_id Table : region - id - name - description - product_id Model : year
public function products()
{
return $this->hasMany(Product::class);
}
public function products()
{
return $this->hasMany(Product::class);
}
Model : product
public function year()
{
return $this->belongsTo(Year::class);
}
public function regions()
{
return $this->hasMany(Region::class);
}
public function year()
{
return $this->belongsTo(Year::class);
}
public function regions()
{
return $this->hasMany(Region::class);
}
Model : region
public function product()
{
return $this->belongsTo(Product::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
Ressource Region :
use App\Models\Region;
Forms\Components\Select::make('year_id')->relationship('product.year', 'year') <--------------- Not working
Ressource Region :
use App\Models\Region;
Forms\Components\Select::make('year_id')->relationship('product.year', 'year') <--------------- Not working
What I need is to display and set selected field for edit.
LeandroFerreira
LeandroFerreira16mo ago
Select::make('year_id')->relationship('year', 'year') ?
Pasteko
Pasteko16mo ago
Yes it's working only for second model, sorry forgot to add the third one.
toeknee
toeknee16mo ago
What’s the third? We need more detail. It doesn’t matter how many relationships you have, providing they work you can harness them with the dot syntax
LeandroFerreira
LeandroFerreira16mo ago
Are you trying to do this?
Card::make()
->relationship('product')
->schema([
Select::make('year_id')->relationship('year', 'year')
])
Card::make()
->relationship('product')
->schema([
Select::make('year_id')->relationship('year', 'year')
])
Pasteko
Pasteko16mo ago
I'm trying to avoid using ->afterStateHydrated, using the chained relations in Region ressource.
Select::make('year_id')->relationship('product.year', 'year')
Select::make('year_id')->relationship('product.year', 'year')
I'm trying to figure out what's wrong from the Region ressource, This works because I'm just getting the value year from the table years?
Tables\Columns\TextColumn::make('product.year.year')->label(Region')->sortable()
Tables\Columns\TextColumn::make('product.year.year')->label(Region')->sortable()
This don't because I'm trying to get all the year values from the table years?
Select::make('year_id')->relationship('product.year', 'year')
Select::make('year_id')->relationship('product.year', 'year')
So the problem is my relation right? I'm learning this relations thing, but i feel kinda lost right now 😦
LeandroFerreira
LeandroFerreira16mo ago
Did you try this?
Card::make()
->relationship('product')
->schema([
Select::make('year_id')->relationship('year', 'year')
])
Card::make()
->relationship('product')
->schema([
Select::make('year_id')->relationship('year', 'year')
])
Pasteko
Pasteko16mo ago
Yes! It's working, thank you, will it work with more dependant selects?
LeandroFerreira
LeandroFerreira16mo ago
Should work You may use Group instead of Card if you want, I think
Pasteko
Pasteko16mo ago
Sorry but I can't find Group in the docs?
awcodes
awcodes16mo ago
Surprised it’s not in the docs but it exists as a layout component. https://github.com/filamentphp/filament/blob/2.x/packages/forms/src/Components/Group.php
GitHub
filament/Group.php at 2.x · filamentphp/filament
Admin panel, form builder and table builder for Laravel. Built with the TALL stack. Designed for humans. - filament/Group.php at 2.x · filamentphp/filament
LeandroFerreira
LeandroFerreira16mo ago
Thank you 🙂
awcodes
awcodes16mo ago
I don’t see any indication that it’s deprecated either. Weird is not in the docs. I use it all the time.
Pasteko
Pasteko16mo ago
The problem I have with Group is that one group only resizes to half of the page, I have create two groups to fill the page. How can I access the select value "year_id" after the two relationships? Livewire returns it inside an array "product".
awcodes
awcodes16mo ago
Access it where? In the form it should just be dot notation ‘product.year_id’. In your component it’s just a php array.
Pasteko
Pasteko16mo ago
Thanks it's working!