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

prastowodwiatmoko
prastowodwiatmoko

Anyone can help me how to Showing Image Attachment From Another Model that has Relation ? Thanks

Rob Ballantyne (Dynamedia)
Rob Ballantyne (Dynamedia)

For a single image attachment

{{ item.relation.image.path }}

For the first of many

{{ item.relation.images.first.path }}

For each of many

{% for image in item.relation.images  %}
    {{ image.path }}
{% endfor %}
prastowodwiatmoko
prastowodwiatmoko

@ro>Rob Ballantyne (Dynamedia) said:

For a single image attachment

{{ item.relation.image.path }}

For the first of many

{{ item.relation.images.first.path }}

For each of many

{% for image in item.relation.images %} {{ image.path }} {% endfor %}

okay thanks , but it still not working, maybe my question not clearly, but i want to explain with example:

  • There's 2 model, Film and Category
  • Every category has a logo (image attachment)
  • In the Film's model, there is a category_id as a foreign key
  • I want to show the film with category's logo in the front end

    {% for film in Films %} {% ???.logo.path %} {% endfor %}

What can I do to showing the logo from category's model ?

Rob Ballantyne (Dynamedia)
Rob Ballantyne (Dynamedia)

Ok, you'll need to do make sure your component is loading the film's you want so you should have something similar to this in your component.

public $films;

public function onRun()
{
    $this->films = Film::with('category', function ($q) {
            $q->with('logo');
        })->get()
}

You'll probably want to add more clauses and pagination rather than getting all, but this should help you start.

And in your twig markup

{% for film in __SELF__.films %}
    {{ film.category.logo.path }}
{% endfor %}

You can also use .thumb(150, 150) in place of .path to resize the image as you like.

On mobile so I'll check this is right later if you have any problems.

1-4 of 4

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