Voyager: BelongsTo Relation is saving as null

Created on 9 Mar 2018  路  22Comments  路  Source: the-control-group/voyager

  • Laravel Version: 5.5.*
  • Voyager Version: 1.0.16
  • PHP Version:7.0.22
  • Database Driver & Version: Mysql 5.7.20

Description:

I think it's a bug. While saving data having relation of BelongsTo, is saved as null.

Steps To Reproduce:

Create two table with belongsTo Relations
1) Customer
column1 : id
columnd2: name
columnd3 : email

2) Customer Documents Table
column1 : id
column2 : customer_id
column3 : documents

Use BreadController while creating bread and try to save customer documents. Column customer_id is saved as null.

After debugging, I found that the below line of code was making the issue.
/tcg/voyager/Http/Controllers/Controller.php

// if the field for this row is absent from the request, continue
            // checkboxes will be absent when unchecked, thus they are the exception
if (!$request->hasFile($row->field) && !$request->has($row->field) && $row->type !== 'checkbox') {
                continue;
            }

this line of code was only checking the request field but not the row type with relationship . So, i modified as

```
// if the field for this row is absent from the request, continue
// checkboxes will be absent when unchecked, thus they are the exception
if (!$request->hasFile($row->field) && !$request->has($row->field) && $row->type !== 'checkbox' && !$row->type=='relationship') {
continue;
}

        if ($row->type == 'relationship' && $options->type != 'belongsToMany') {
            $row->field = @$options->column;
        }

// dd($request->all());

        $content = $this->getContentBasedOnType($request, $slug, $row);

```

I will send PR if the bug is confirmed.
Thank you

help wanted possible bug

Most helpful comment

Just fixed that issue by adding the question_id as hidden field and giving visibility to everything

All 22 comments

@marktopper i got this bug while i was doing one project and fixed that by changing the core. If anybody can confirm it then i will send the PR which is ready. :)

This _might_ get fixed within https://github.com/the-control-group/voyager/pull/2763
Can you test that?

For me it doesn't post the question_id at all
SQLSTATE[HY000]: General error: 1364 Field 'question_id' doesn't have a default value (SQL: insert into answers (description, other, updated_at, created_at) values (Facebook, , 2018-03-09 15:48:24, 2018-03-09 15:48:24))

Just fixed that issue by adding the question_id as hidden field and giving visibility to everything

@emptynick Code looks good. Will test it and will let you guys know.

Any update on this?

Legend. Was pulling my hair out with this and it solved my issues. Would be great to see this as a PR and merged

I think this problem still exist on ver. 1.1.3
I need to add the field of the relation as a hidden field to ensure the value is saved in the bread

@cleyan @marktopper this bug doesn't exist in ^1.1. If any one is not able to save the belongs to relation then please confirm if the visibility of the (reference column) field in bread is at least "Add" and "Edit"

Closing as the issue seems resolved in newer versions.
Please let us know if not.
Also make sure the referencing columns are writable as described by @DrudgeRajen

@cleyan @marktopper this bug doesn't exist in ^1.1. If any one is not able to save the belongs to relation then please confirm if the visibility of the (reference column) field in bread is at least "Add" and "Edit"

visibility could be 1 ? right ?

I confirm this is still present in 1.1.11
I had to create another field as "hidden" containing "MODEL_id" to workaround it

This works

I had to create another field as "hidden" containing "MODEL_id" to workaround it

But also check the correct field value for:

Which column from the Model is used to reference the Other Model

Kind of the same issue in 1.3.1. When changing data on a relationship, the data is not saved and the old value is still present. After creating the hidden field, the data is correctly saved.

@luukschakenraad if you have a problem with 1.3.1 please check if your templates are updated and then open another issue.

Just fixed that issue by adding the question_id as hidden field and giving visibility to everything

As simple as this may sound, this fixed the issue. I had to change the foreigntable_id to hidden and this worked mysteriously after being on this issue for about 3 hours.

image

@rotimi-best it doesn't need to be hidden but you need to check add and edit.
I explained how it works in this PR #4729

@rotimi-best it doesn't need to be hidden but you need to check add and edit.
I explained how it works in this PR #4729

I read your explanation but it doesn't say expressly what I should do in place of making it hidden. Firstly I don't even need it to be visible because the relationship created in the BREAD can display the column I chose to see (i.e name) in the browse page which is enough for me.

This is your explanation:
In a BelongsTo relationship the foreign key field decides if value can be saved on add or edit and applies validation rules for everything else, like visibility in browse and edit, the relationship field is used.

@rotimi-best It doesn't say it because the foreign key field associated with a belongsTo relationship it's not displayed in browse, edit-add or show view only the relationship is, removeRelationshipField is the method that removes it in VoyagerBaseController.

Are you using a custom controller?

@MrCrayon I created that table and bread all from Voyager and I created a controller which implement the view and show methods.

@rotimi-best you need to call removeRelationshipField inside your show method to solve the problem.
This is not something that happens in Voyager but only in your custom controller.

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

MikadoInfo picture MikadoInfo  路  3Comments

raoasifraza1 picture raoasifraza1  路  3Comments

Nagendra1421 picture Nagendra1421  路  3Comments

wislem picture wislem  路  3Comments

popica80 picture popica80  路  3Comments