I have the following tables:
entries
- id
- category_id
- name
field
- id
- category_id
- name
field_values
- id
- entry_id
- field_id
- value
category
- id
- name
Each Category has certain fields. If I add an entry, I choose the category and I can enter the values for the field for the entry.
Let's say we have the Category "Venues". Venues has the custom field named "Capacity". If I add a news entry, I need to fill the field "Capacity".
This is how I read the fields and their values for an entry:
@foreach($data->field_values as $values)
{{$values->field->name}}: {{$values->value}}
@endforeach
I can only access the field table through the "field_values" table. Isn't it possible to access field directly from entry maybe with an hasManyThrough ?
This (in Entry Model):
public function fields()
{
return $this->hasManyThrough('App\Field', 'App\FieldValues');
}
throws the following mysql error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fields.field_values_id' in 'on clause' (SQL: select `fields`.*, `field_values`.`entry_id` from `fields` inner join `field_values` on `field_values`.`id` = `fields`.`field_values_id` where `field_values`.`entry_id` in (3))
Why I need to add a field_values_id to Field? Isn't there a different approach?
I just want to foreach through entry->field and not do it by entry_field_values->field