Closed thill0822 closed 6 years ago
I think, relationships must have attributes as other fields... Currently I can't add to relationship custom attribute onlyRole
, but I want it.
@ArtemMolotov, that is unrelated to the issue topic. Please open your own issue
Playing around with this a little I found if I manually added the attributes to the database, some pulled through (such as display
) but others didn't, such as default
. Also saving the BREAD table overwrites this field.
Just adding a perspective to this: I am trying to add a relationship to the current user, that is, I would like to define the relationship to the user and have it automatically set the value to the current user and not allow it to be modified, BUT it should display the relationship's display field when browsing or viewing the record, and I think it should display as disabled in edit mode. I obviously am speaking of a particular type of relationship but I think these capabilities could apply to most BelonsTo relationships in that a default value should be possible.
One thing I attempted that did not work was to add the following JSON option to the column that the BelongsTo relation was set for, in my case it was against the user table using a column called user_id and in the JSON option for user_id I tried to add { "default" : "{{ Auth::id() }}" } in hopes that it would default the user_id column to the current user's id and that would then result in that being what the relationship drop down defaulted to.
No joy though. Just thought I would pass along what I tried and what I was thinking...
Voyager does not currently support default values for relationship fields. Here is the relevant line: https://github.com/the-control-group/voyager/blob/1.0/resources/views/formfields/relationship.blade.php#L35
Specifically this snippet:
@if($dataTypeContent->{$options->column} == $relationshipData->{$options->key}){{ 'selected="selected"' }}@endif
You'll notice that it's comparing the column
option from $dataTypeContent
to the key
option from $relationshipData
. Neither of these options are holding a configurable default value.
Now, we may support this in the future, but likely not in v1.0.*.
@stevenhillsr, for your issue, trying to set it to the current user, you could use events and override the value. Then just don't add the field to the form and it'll be set automatically. Feel free to open a separate issue or (preferably) join our Slack group for further assistance.
Thanks @fletch3555 for the update. I'd love to see it added in future, but can understand why it isn't out of the box.
This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.
Can you set a default value for the dropdown that's autogenerated by a 'Belongs To' relationship?
For example, there are two roles (Admin & User). I'd like it to default to User instead of Admin.
You also can't set display attributes for these fields.