Voyager: belongsTo Relationship bug Voyager 1.2

Created on 24 Mar 2019  路  10Comments  路  Source: the-control-group/voyager

Version information

  • Laravel: v5.7.#
  • Voyager: v1.2.#
  • PHP: 7.#

Description


BelongsTo relationship will not accept or store the relationship values.

Bug fix:
Path: resources/views/formfields/relationship.blade.php
Change
` class="form-control select2-ajax" name="{{ $options->column }}"
data-get-items-route="{{route('voyager.' . $dataType->slug.'.relation')}}"
data-get-items-field="{{$row->field}}"

`

To

` class="form-control select2-ajax" name="{{ $relationshipField }}"
data-get-items-route="{{route('voyager.' . $dataType->slug.'.relation')}}"
data-get-items-field="{{$row->field}}"

`

Finally, the .select2-ajax doesn't add a "none" option when a selection is being made. This means that a user will not be able to deselect an option if they decide they want to.

possible bug

Most helpful comment

The relationship bug is a result of sending the wrong field name to the controller.

name="{{ $options->column }}"
should be:
name="{{ $relationshipField }}"

the $relationshipField variable was created but not used. That is the correct fix for the bug, toggling all options on the BREAD shouldn't be necessary.

All 10 comments

I'm also experiencing this issue.

@suxur @tunapiq You guys tried to enable all BREAD actions for the field that refers to the other table?
I was experiencing the same issue until saw this and was able to make it work.

The relationship bug is a result of sending the wrong field name to the controller.

name="{{ $options->column }}"
should be:
name="{{ $relationshipField }}"

the $relationshipField variable was created but not used. That is the correct fix for the bug, toggling all options on the BREAD shouldn't be necessary.

PR's welcome @tunapiq

@fletch3555 Just made the PR with the fix @tunapiq suggested.

If anyone needs it, here is a fix for the missing "None" option for the select2-ajax (relationship select drop-downs), change

$results = [];
to

$results = [
    [
        'id'   => '',
        'text' => "--None--",
    ]
];

in the method VoyagerBaseController@relation. Doing that will give a user the ability to deselect an option if they so decide it.

@tunapiq, It's not as simple as that. What if the field isn't nullable? This would allow a user to select an empty value, and when saving, it would error.

Since the problem only occurs with select drop-downs and not select multiple. An alternate solution could be

             $results = [];
            if(
               $row->details->type === "belongsTo"
               && DB::connection()->getDoctrineColumn($dataType->name, $row->details->column)->getNotnull() === false
             ){
                $results [] = ['id'   => '', 'text' => "--None--"];
              }

the $relationshipField variable was created but not used. That is the correct fix for the bug, toggling all options on the BREAD shouldn't be necessary.

After fiew checks I've seen that if you do so the Controller try to use the field name as the varable to store the value instead of the key:
https://github.com/the-control-group/voyager/blob/1.2/src/Http/Controllers/Controller.php#L113

So you should add another case for the belongsTo relationship on top:
https://github.com/the-control-group/voyager/blob/1.2/src/Http/Controllers/Controller.php#L61
(Otherwise the value is replaced with none due to the fact that $content would be empty.

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.

Was this page helpful?
0 / 5 - 0 ratings