Select multiple issue

I have this issue where I select certain people, save, and the order seems correct in the selection. After refreshing, the order changes. Do you have any idea what could be causing this?
27 Replies
Dennis Koch
Dennis Koch16mo ago
Please show some code. Is it a relationship?
DianaMujoiu
DianaMujoiuOP16mo ago
Fieldset::make('Staff')
->schema([
Select::make('staff')
->options(Staff::all()->pluck('first_name', 'id'))
->relationship('staff', 'first_name')
->preload()
->multiple()
->label('')
->columnSpanFull(),
])->columnSpan(['lg' => 1]),
Fieldset::make('Staff')
->schema([
Select::make('staff')
->options(Staff::all()->pluck('first_name', 'id'))
->relationship('staff', 'first_name')
->preload()
->multiple()
->label('')
->columnSpanFull(),
])->columnSpan(['lg' => 1]),
and the model
public function staff()
{
return $this->belongsToMany(Staff::class, 'guest_experience_staff');
}
public function staff()
{
return $this->belongsToMany(Staff::class, 'guest_experience_staff');
}
Yes, it's a relationship
Dennis Koch
Dennis Koch16mo ago
As long as there is no orderBy I don't think there is any guaranteed ordering.
DianaMujoiu
DianaMujoiuOP16mo ago
Where should I put orderBy?
Dennis Koch
Dennis Koch16mo ago
You can either order your relationship (if you always want it ordered) or you can pass a 3rd param to relationship() to adjust the query
DianaMujoiu
DianaMujoiuOP16mo ago
And will this change maintain the order of the elements based on how they are selected?
Dennis Koch
Dennis Koch16mo ago
Not sure if they are saved in the order you add them. You could order them by their pivot id. But might not work
DianaMujoiu
DianaMujoiuOP16mo ago
In this case, would it be a better option to use a repeater with a select inside it instead of this multiple select?
Dennis Koch
Dennis Koch16mo ago
If you really need to the order in which they were added you probably need a JSON column or a relationship that has a order column
DianaMujoiu
DianaMujoiuOP16mo ago
Ok, thank you for your help One more question, please. I've added an "order" column to my pivot table "guest_staff". Is it possible to create a checkbox list with a select staff relationship and also, for all the staff person to have a field where to add the order?
TableRepeater::make('guestExperienceStaff')
->headers(['Staff', 'Order'])
->relationship()
->schema([
Forms\Components\Grid::make(2)
->schema([
Select::make('staff_id')
->label('')
->relationship('staff', 'first_name')
->required(),
TextInput::make('order_column')
->label('')
])
]),
TableRepeater::make('guestExperienceStaff')
->headers(['Staff', 'Order'])
->relationship()
->schema([
Forms\Components\Grid::make(2)
->schema([
Select::make('staff_id')
->label('')
->relationship('staff', 'first_name')
->required(),
TextInput::make('order_column')
->label('')
])
]),
I did in this way, but the repeater displays only the last item
Dennis Koch
Dennis Koch16mo ago
Why do you use a TableRepeater instead of a Repeater with orderable()?
DianaMujoiu
DianaMujoiuOP16mo ago
I added this now but can't reorder the repeater items and still see only the last or the first item, although in the db I have all the items
Dennis Koch
Dennis Koch16mo ago
Should give you an error actually, since it's ->reorderable(). Check the docs on reodering: https://filamentphp.com/docs/3.x/forms/fields/repeater#reordering-items
still see only the last or the first item
Can you share the code for guestExperienceStaff relationship?
DianaMujoiu
DianaMujoiuOP16mo ago
class GuestExperienceStaff extends Pivot
{
use HasFactory;

public function guest(): BelongsTo
{
return $this->belongsTo(GuestExperience::class);
}

public function staff(): BelongsTo
{
return $this->belongsTo(Staff::class);
}
}
class GuestExperienceStaff extends Pivot
{
use HasFactory;

public function guest(): BelongsTo
{
return $this->belongsTo(GuestExperience::class);
}

public function staff(): BelongsTo
{
return $this->belongsTo(Staff::class);
}
}
I am using Filament v2
Dennis Koch
Dennis Koch16mo ago
That's the Pivot class. But what is the relationship definition on your model?
DianaMujoiu
DianaMujoiuOP16mo ago
public function guestExperienceStaff(): HasMany
{
return $this->hasMany(GuestExperienceStaff::class);
}
public function guestExperienceStaff(): HasMany
{
return $this->hasMany(GuestExperienceStaff::class);
}
sorry
Dennis Koch
Dennis Koch16mo ago
Can you check what the relation returns somewhere in your Laravel app or in Tinker:
dd(
YourModel::find('your_id')
->guestExperienceStaff
)
dd(
YourModel::find('your_id')
->guestExperienceStaff
)
DianaMujoiu
DianaMujoiuOP16mo ago
0 => App\Models\GuestExperienceStaff {#2141 ▼
#connection: "mysql"
#table: "guest_experience_staff"
#primaryKey: "id"
#keyType: "int"
+incrementing: false
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:5 [▼
"guest_experience_id" => "6391f666-a67f-4e77-b8c3-a9bf4ebc8902"
"staff_id" => "85291b94-3a8b-494b-b7c3-a1d8875d93a2"
"created_at" => "2023-08-31 07:54:07"
"updated_at" => "2023-08-31 07:54:07"
"order_column" => 1
]
#original: array:5 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: []
+pivotParent: null
#foreignKey: null
#relatedKey: null
1 => App\Models\GuestExperienceStaff {#2142 ▼
#connection: "mysql"
#table: "guest_experience_staff"
#primaryKey: "id"
#keyType: "int"
+incrementing: false
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:5 [▶]
#original: array:5 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
}
0 => App\Models\GuestExperienceStaff {#2141 ▼
#connection: "mysql"
#table: "guest_experience_staff"
#primaryKey: "id"
#keyType: "int"
+incrementing: false
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:5 [▼
"guest_experience_id" => "6391f666-a67f-4e77-b8c3-a9bf4ebc8902"
"staff_id" => "85291b94-3a8b-494b-b7c3-a1d8875d93a2"
"created_at" => "2023-08-31 07:54:07"
"updated_at" => "2023-08-31 07:54:07"
"order_column" => 1
]
#original: array:5 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: []
+pivotParent: null
#foreignKey: null
#relatedKey: null
1 => App\Models\GuestExperienceStaff {#2142 ▼
#connection: "mysql"
#table: "guest_experience_staff"
#primaryKey: "id"
#keyType: "int"
+incrementing: false
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:5 [▶]
#original: array:5 [▶]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
}
Dennis Koch
Dennis Koch16mo ago
Hm. Looks good. What's the current Repeater code?
DianaMujoiu
DianaMujoiuOP16mo ago
Repeater::make('guestExperienceStaff')
->relationship()
->schema([
Select::make('staff_id')
->label('')
->relationship('staff', 'first_name')
->required(),
TextInput::make('order_column')
->label('')
]),
Repeater::make('guestExperienceStaff')
->relationship()
->schema([
Select::make('staff_id')
->label('')
->relationship('staff', 'first_name')
->required(),
TextInput::make('order_column')
->label('')
]),
I only removed the orderable()
Dennis Koch
Dennis Koch16mo ago
Also looks good. No idea why it only shows one guestExperienceStaff Can you show a screenshot?
DianaMujoiu
DianaMujoiuOP16mo ago
Dennis Koch
Dennis Koch16mo ago
No idea what's happening sorry. Is your v2 version up-to date?
DianaMujoiu
DianaMujoiuOP16mo ago
yes, is up to date 😅
DianaMujoiu
DianaMujoiuOP16mo ago
I believe the issue is caused by the fact that a new entry is being created in the table for each person, instead of a single JSON entry as the Repeater normally functions
No description
Dennis Koch
Dennis Koch16mo ago
No, that's how it should be. It's a relation
DianaMujoiu
DianaMujoiuOP16mo ago
Yes, I know that this is how data is normally added for relationships. However, I thought that it might not be suitable for the Repeater in this case
Want results from more Discord servers?
Add your server