Voyager: belongsTo Relationship bug Voyager 1.2.7 after changing from $options->column to $relationshipField

Created on 26 Sep 2019  路  7Comments  路  Source: the-control-group/voyager

Version information

Laravel: v5.8.#
Voyager: v1.2.7
PHP: 7.1

  • Database: MySQL v5.7.26

Description:

After changing the fields relationship in Path: resources/views/formfields/relationship.blade.php

From:

The data is not sent to the database and the belong to relationship is not created
but if I change the .select2-ajax from {{ $relationshipField }} to {{ $options->column }} it functions properly and the relationship is created

Additional context

Relation screenshot attached
belongtoRelationship

All 7 comments

I don't think this is a bug like I don't think #4031 was a bug, but with #4228 was actually solved another problem and now it's possible to use validation for belongsTo relationship.

Voyager can use both values in name to save, the only thing is you need to enable edit in the correct field in your BREAD configuration.

In short enable Edit for project_parent_id and it should work.

The thing is that project_parent_id edit is enabled in BREAD as shown in the attachment
Screenshot_2019-09-27 Edit BREAD for projectpages table

also when {{ $relationshipField }} set the field name would be "projects projectpage_belongsto_project_relationship" and then the id would not be saved in the project_parent_id field

but if {{ $options->column }} isset the field name is "project_parent_id" and the id is saved.

I got it backward I thought last PR was changing it from the long form projects projectpage_belongsto_project_relationship to project_parent_id, but was the other way around so I retract my claim of any benefit on that PR.

I still stand that both ways should work, if you check
https://github.com/the-control-group/voyager/blob/015c517a1e52ee93a6f83a24a601f50cb0b92cf1/src/Http/Controllers/Controller.php#L69
you'll see how value is saved in the right field even if sent with _wrong_ name.

Now I see what's this bug is about and also #4031 and has nothing to do with the name chosen.

The problem is that values saved are assigned with a foreach of data rows:
https://github.com/the-control-group/voyager/blob/015c517a1e52ee93a6f83a24a601f50cb0b92cf1/src/Http/Controllers/Controller.php#L55

When there is a belongsTo there are two rows that can potentially assign the value and one of them is going to be set as NULL, so your problem right now is that project
_parent_id comes after the relationship so it set value back to NULL.

You can fix this in two ways:
Move the relationship field after project_parent_id
Even better remove the edit and add check for project_parent_id

I think this problem should be addressed but I don't know how BREAD is going to change in the future releases.

YES :)

I can confirm by moving the relationship below project_parent_id or vice-versa the value is saved in its field.

Thank you for your follow up
Screenshot_2019-09-27 Edit BREAD for projectpages table(1)
and Thumb up

You are welcome.
I think it's better if you set again the edit and add for project_parent_id, it will still work and if my PR is merged you won't have to change anything after and there will be no problem about ordering either.

Great will do, Thanks again :)

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

Related issues

Nagendra1421 picture Nagendra1421  路  3Comments

kevinjon27 picture kevinjon27  路  3Comments

TXRRNT picture TXRRNT  路  3Comments

abacram picture abacram  路  3Comments

wp-src picture wp-src  路  3Comments