jeudi 8 février 2018

Laravel Eloquent images/pages relationship setup

I'm moving over to Laravel and figured the best way to learn is to rebuild my own CMS in Laravel but I'm struggling with the following:

I'm using Laravel 5.4

I have five tables (with corresponding models set up):

images (id, type_id, item_id)
images_types (id)
images_types_templates (type_id, template_id)
pages (id, template_id)
templates (id)

I'm trying to set up an eloquent relationship so that I can run this in Tinker:

Page::with(['images'])->first()

And see the related images for this page(item_id) AND template_id

So far I can only retrieve items with this in my model:

public function images()
    {
        return $this->hasMany('App\Image', 'item_id');
    }

This does work, however it only retrieves a pages.id that matches an images.item_id

Whereas I need to check that the images.type_id matches the current page. This gets set from the pages table (pages.template_id, which needs to check the images_types_templates.template_id to see if this sort of image is allowed on this page)

I was hoping I could run Page::with(['images']) and only see images relating to this page and template_id. The item_id field in the images table could relate to any other part of the website, not just pages which is why it is item_id and not page_id

Hope that makes sense



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire