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.
In phpmyadmin the data is stored correctly when i edit the BREAD.
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.
Most helpful comment
I'll keep it open since the workaround it's not a real fix.