Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wolle404's avatar

Multiple Tables/Models in one Repository

Hey there,

I'm working with Repositories, and I was wondering how you handle multiple Tables. Example: Model Product with Table 'products' Model ProductImages with Table 'product_images' Model ProductOptions with Table 'product_options'

Do I now only have the 'ProductRepository' where I create/update/delete the product with its images and options or do I swap each table into one repository (ProductRepository, ProductImageRepository, ProductOptionRepository), where I create/update/delete each product/image/option?

If I do like the last example with separate Repositories: Do I couple each call in the controller or can I create like a layer between controller and repository, where I just pass all three things and the class will call each repository with it's function?

Thank you!

0 likes
4 replies
khaledSMQ's avatar

Hi , @wolle404 am not sure if i understand your question well, but i think you need to have a relation in your module like

class ApiTrackers extends Eloquent
{
......
    
 public function Image()
    {
        return $this->belongsToMany(ProductImage::class, 'products_image','product_id','image_id');
    }
}

// you table products_image can also handle options or settings of image instance as well
like you can create a table products_image
product_id, image_id, settings, other_settings, path......

and in your repo you can attach or create you dedicated methods

wolle404's avatar

@khaledSMQ: I have all relations created, I just only want to know how I handle this within a repository.

I have all those relations but how do I use it within my repository.

@Ruffles: Thank you for the link! I checked that out but it didn't answers my question.

How can I use the example "$post->addComment(...)" with a repository. Cause I would do something like this "$repo->addCommentForPost($post_id, $comment)"

So if you would split all things into each classes (repositories and controller) how do you collect all things to one? Are you making a class where you combine all things (images and options) if you call something like "getData()"? (Which returns the product with all images and options and basic data)

spekkionu's avatar

You don't have to have one repository for each model.

As long as the methods in your repository logically make sense they can interact with whatever models and tables they need.
It is way more efficient to have your relationships be returned along with the main object rather than separately calling methods in different repositories.

Part of the purpose for having a repository in the first place is as an abstraction over your models which with active record based models like eloquent match the structure of your database pretty much exactly. Enforcing each repository to only interact with only one model type throws away that abstraction is basically no different than if you just added the methods directly on the models.

3 likes

Please or to participate in this conversation.