This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

thomas15915526
thomas15915526

I have 2 tables albums and images

albums
========
id
title
slug

images
========
id
img
album_id

I'm trying to get a front end relationship between these using the album_id as the relation field.

I have declared the relationship in both models:

Album

public $belongsTo = [
    'albums' => ['Acme\Library\Models\Album']
];

Image

public $hasMany = [
   'images' => [ 'Acme\Library\Models\Image' ,'key'=>'album_id'],
];

And in my albums/:slug details page I have used the twig code

{{ images.img }}

but get no output displayed nor do i get any errors.

Am I missing anything here in getting this to work?

Thanks!

philipptempel
philipptempel

Guessing from your code, you have set up the relation incorrectly. In your Image model you should have a $belongsTo and in the Album model a $hasMany. Now that aside, you cannot do {{ images.img }} in your twig code because that is invalid syntax. It is no surprise that it doesn't render anything because that is the way Twig works. It catches all errors rather than displaying them. What you want to do first of all is have a look at the structure of images in your twig by doing {{ dump(images) }}. You should see images being an array. Thus you need to loop over it to print {{ image.img }}}.

Hope that helped.

thomas15915526
thomas15915526

ah thanks so now I have the below and when I {{ dump(images) }} I get

Object variables NULL 0 NULL

Any Ideas why the relationship is not being picked up?

Album

public $hasMany = [
'images' => [ 'Acme\Library\Models\Image' ,'key'=>'album_id'],
];

Image

 public $belongsTo = [
'albums' => ['Acme\Library\Models\Album']
];

album-details.htm

{{ dump(images) }}
{% for image in images %}
{{image.id|raw }}

{% endfor %}

if I run a dump on {{ dump(record) }}I get

Object variables
Acme\Library\Models\Album

bootValidation()    method  Boot the validation trait for this model. 
forceSave() method  Force save the model even if validation fails.
validate()  method  Validate the model instance
 isAttributeRequired()  method  Determines if an attribute is required based on the validation rules.
errors()    method  Get validation error message collection for the Model
validating()    method  Create a new native event for handling beforeValidate().
validated() method  Create a new native event for handling afterValidate().
rules   array(0)    
timestamps  boolean 
table   string  
relationConfig  string  
hasMany array(1)

I can see using the builder plugin that record is defined {% set records = builderList.records %}

Does albums/images need to be defined in this manner also?

Last updated

thomas15915526
thomas15915526

I have now been able to get this working

The relationships are setup correctly. I just needed to write the correct twig syntax

album-details.htm page

{% if record %}
{{ attribute(record, displayColumn) }}
{% for image in record.images %}
{{ image.img }}
{% endfor %}
philipptempel
philipptempel

Glad you got it sorted out. Just a quick question: Why do you use {{ attribute(record, displayColumn) }}? I am aware of the twig-method attribute() just not sure what it does here (and where displayColumn is defined).

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.