Voyager: Belongs to Many relationship

Created on 21 Dec 2017  路  6Comments  路  Source: the-control-group/voyager

  • Laravel Version: 5.5.*
  • Voyager Version: 1.0
  • PHP Version:7.0.*
  • Database Driver & Version: 5.7.*

Description:

Sql syntax error for Belongs to many relationship.

`SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select `id` from `classes` inner join `item_class` on `classes`.`id` = `item_class`.`class_model_id` where `item_class`.`item_id` is null)`

_I think the selection should be classes.id ._

Steps To Reproduce:

  1. Create table classes with model App\ClassModel .
  2. Create table items with model App\Items .
  3. Create pivot table item_class
  4. Add belongsToMany relationship after creating BREAD as
    class = Classes
    model = App\ClassModel
    pivot_table = item_class
    display the classes = name
    store the classes = id

Most helpful comment

Fixed by editing resources/views/formfields/relationship.blade.php .

Changed $options->key to $options->table.'.'.$options->key in pluck function argument.

The resulting value :
$selected_values = isset($dataTypeContent) ? $dataTypeContent->belongsToMany($options->model, $options->pivot_table)->pluck($options->table.'.'.$options->key)->all() : array();

All 6 comments

Fixed by editing resources/views/formfields/relationship.blade.php .

Changed $options->key to $options->table.'.'.$options->key in pluck function argument.

The resulting value :
$selected_values = isset($dataTypeContent) ? $dataTypeContent->belongsToMany($options->model, $options->pivot_table)->pluck($options->table.'.'.$options->key)->all() : array();

Is there any fix already for this issue on the latest update? I have tried to update to the latest version but this issue still persists. Why is this issue closed?

Yes, but it either hasn't been merged yet or has but hasn't been released yet (Not sure offhand, and I'm not at a computer to check). It'll be going in the next release

@gentra I just closed because there was no response. The cause and solution to the error is as stated.

@Martian2050 hmm well yeah, I cannot really use that solution since it means I need to modify the library's code inside the vendor folder. However, I'm using Elastic Beanstalk where the libraries code get regenerated on every deploy so it won't work for my case.

The only temporary solution I do now is by using a different primary-key name for the tables so it won't conflict each other.

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