dimanche 29 novembre 2015

Creating a manageable structure for Eloquent in L5

I need to use a Photo in several different places and cannot quite figure out what would be the best approach.

So basically, there is a gallery page on the website that shows a bunch of Photos. There is also a portfolio page, where each project has many Photos. There is a blog section too, and each blog has many Photos.

The common thing with these photos are that they have an image_url and a caption.

Project and Blog are Eloquent models and Photo model is a polymorphic model.

Project and Blog have:

public function photos()
{
    return $this->morphMany('App\Photo', 'imageable');
}

And the Photo model has:

public function imageable()
{
    return $this->morphTo();
}

This bit works fine. But my problem is that I want to attach this Photo model to the gallery as well. I'm thinking the two options I have, are:

  1. Create a Gallery model, which will have just one entry, as the gallery is just one page. And the model would have the same photos() method as above.
  2. When loading up the gallery page, retrieve all the photos that have imageable_id and imageable_type set to null. (This would mean that the photos that do not belong to neither Project nor Blog).

But I think there's a different, better approach that I cannot see. Any suggestions?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire