Select field hasMany relation

Hello all, I am trying to achieve a select field relation with a hasMany relation. I have one Model called Content and one model ContentVisibility ContentVisibility holds a content_id, with a type field which can be either be BRANCH or COMMUNITY in Content.php I have relations defined as; public function visibilities(): HasMany { return $this->hasMany(ContentVisibility::class, 'content_id', 'id'); } also in ContentVisibility.php as; public function content(): BelongsTo { return $this->belongsTo(Content::class, 'id', 'content_id'); } I want to Make two select fields in "EditContent" resource which edits the relations. I came up like these; Select::make('branch_visibility_id') ->label('Show only to this branches...') ->multiple() ->options( ... ) ->relationship('visibilities', fn (Builder $query) => $query->where('type', ContentVisibilityTypeEnum::BRANCH)) ->searchable(), Select::make('community_visibilities') ->label('Show only to this communities...') ->multiple() ->options(...) ->relationship('visibilities', fn (Builder $query) => $query->where('type', ContentVisibilityTypeEnum::COMMUNITY)) ->searchable(), But as far as I understand "relationship" for select searches for a BelongsToMany relation, which I think it is not suitable for this situation. Did I missed something? Thanks.
10 Replies
Dan Harrin
Dan Harrin•2y ago
i think this would work best as a belongstomany
onursahindur
onursahindurOP•2y ago
Okay thanks, I changed it to a belongsToMany relation, now I have another little question 🙂 How can I add a default value before save in Select? My select now looks like this; Select::make('branch_visibilities') ->label('Şubeler') ->relationship('branch_visibilities', 'object_id') ->multiple() ->options(fn () => Branch::where('corp_id', $authUser->corp_id) ->get() ->pluck('title', 'id') ) ->searchable() but when I click save button on EditRecord, it gives me SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "type" of relation "content_visibilities" violates not-null constraint DETAIL: Failing row contains (13, 1, 1, null, null, null). since a type is required in content_visibilities table
onursahindur
onursahindurOP•2y ago
I try to achieve it with mutateFormDataBeforeSave function, but the error was thrown before the code reaches here
Dan Harrin
Dan Harrin•2y ago
mutate isnt gonna work here relationships get saved before that point maybe make a pivot model, add it to the relationship definition, and then create an observer for it? then you can use the creating() method on the observer to fill in the type
onursahindur
onursahindurOP•2y ago
Is it a good way to achieve it via this callback?
onursahindur
onursahindurOP•2y ago
YES! Finally đź‘Ż I Managed to do it via a deep search in discord, I found an answer to yours. (https://discord.com/channels/883083792112300104/1085981789887143966/108598628176024385) So this helped me, maybe it helps anybody else; Select::make('branch_visibilities') ->label('Ĺžubeler') ->relationship('branch_visibilities', 'object_id') ->multiple() ->options(fn () => Branch::where('corp_id', $authUser->corp_id) ->get() ->pluck('title', 'id') ) ->saveRelationshipsUsing(static function (Select $component, $state, $get) { $component->getRelationship()->syncWithPivotValues($state, ['type' => ContentVisibilityTypeEnum::BRANCH]); }) ->searchable()
Dan Harrin
Dan Harrin•2y ago
nice!
Lara Zeus
Lara Zeus•9mo ago
when dose saveRelationshipsUsing triggered? anyone know? its never dding there!
LeandroFerreira
LeandroFerreira•9mo ago
this?
Select::make('xx')
->relationship(...)
->saveRelationshipsUsing(function () {
//...
})
Select::make('xx')
->relationship(...)
->saveRelationshipsUsing(function () {
//...
})
Lara Zeus
Lara Zeus•9mo ago
ya but not been triggered for some reasons not on edit or create!
Select::make('xx')
->relationship(...)
->saveRelationshipsUsing(function () {
dd(9);
})
Select::make('xx')
->relationship(...)
->saveRelationshipsUsing(function () {
dd(9);
})
after some source digging I ended up using saveRelationshipsBeforeChildrenUsing
Want results from more Discord servers?
Add your server