Voyager: "Has one" relatinship displays no results

Created on 10 Sep 2020  路  3Comments  路  Source: the-control-group/voyager

Version information

  • Laravel: v 7.28.0
  • Voyager: v 1.4.x-dev
  • PHP: v 7.4.8
  • Database: MySQL v 5.7.24

Description:

I have two tables products and prices related with a one to one relationship,
products is the parent table and is referenced in the prices table with the column named id_product.
I made a has one relationship using the admin panel in the products table, and a belongs to many relationship in the prices table.
In the product BREAD I want to display the price if available, and in the prices table I want to display an choose the product name (in the future the sku).
The prices BREAD with the belongs to many relationship works as intened but the products table always displays no results.

Screenshots:

products table:

products_table

products relationship has one related to prices:

products_relationship

products BREAD displays no results for prices price column:

products_bread

prices table:

prices_table

prices relationship belongs to products:

prices_relationship

prices BREAD displays products name column like i wanted:

prices_bread

In phpmyadmin the data is stored correctly when i edit the BREAD.

addressed bug

Most helpful comment

I'll keep it open since the workaround it's not a real fix.

All 3 comments

The problem is that when you select HasOne relationship the Store as should be the key of the parent Model to store in related Model.
In your case it should be Product hasOne Price, Display the Prices price, Store the Product (not Prices) id_product, but since that list is made up with related model table (prices) columns it works only while using standard primary key names like id and you cannot currently choose the right one.

Quickest workaround is to create a custom view for your field, set in Relationship Details

{
    "view": "my_view"
}

Then create your custom view, this is the original one:

@php
    $relationshipData = (isset($data)) ? $data : $dataTypeContent;

    $model = app($options->model);

    $query = $model::where($options->column, '=', $relationshipData->{$options->key})->first();

@endphp

@if(isset($query))
    <p>{{ $query->{$options->label} }}</p>
@else
    <p>{{ __('voyager::generic.no_results') }}</p>
@endif

You need to change {$options->key} in id_product or {$options->column}

It works thank you

I'll keep it open since the workaround it's not a real fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevinjon27 picture kevinjon27  路  3Comments

IvanBohonosiuk picture IvanBohonosiuk  路  4Comments

winex01 picture winex01  路  3Comments

craigb88 picture craigb88  路  3Comments

duongsieu picture duongsieu  路  3Comments