Support the ongoing development of Laravel.io →
IOC Architecture

I know I'm overthinking this, but I wanted feedback from people with more experience. I'm building an application that abstracts the Eloquent database access via interfaces and repository-classes (this after reading the Laravel book "From Apprentice to Artisan"). So for example, my "MovieController" references a "MovieRepositoryInterface" as a requirement to its constructor, that interface defines the methods used by the object, and then a "MovieRepository" class is tapped as the one to be used for any requests for that particular interface (via a custom provider and a call to App::bind()).

This pattern works really well for simple objects: the classes are easy to mock/test, easy to troubleshoot, etc. And it works really well when you are just VIEWING the object in question. Relations can be easily resolved by defining the related classes in Eloquent.

However, things start to get complicated when I have to CREATE these objects and there are related objects involved. What should that look like? I've got some objects that may require 3, 4, or 5 related objects corresponding to various foreign keys in the database. When my webform needs to create an object like that, the user needs to select a valid relation so that its foreign key can be stored. For example, a "Movie" object might need foreign keys for "director_id", "studio_id", "language_id", etc.

I can see two possible solutions to this predicament:

Solution 1: Inject all the classes you need. All of them. Even if there are a lot. That might end up looking something like this in my controller:

    class MyController
    {
    public $movie;
    // ... etc...
    
    public function __construct(MovieRepositoryInterface $movie, LanguageRepositoryInterface $lang, DirectorRepositoryInterface $dir, StudioRepositoryInterface $studio, ...etc... )
    {
        $this->movie = $movie;
        $this->lang = $lang;
        $this->dir = $dir;
        $this->studio = $studio;
        // ... etc ...
    }
    
    public function getCreate() {
        return View::make('movie.create')
            ->with('languages', $this->lang->all());
            ->with('directors', $this->dir->all());
            ->with('studios', $this->studio->all())
            // ... etc...
            ;
    }

Solution 2: Request related data via Ajax. Keep the controller simple: it requires only the interface for the base class it represents. All other data is requested via Ajax in the view layer.

These represent 2 very different approaches. I prefer to get all the data I need in one request... but sometimes the UI requires Ajax, e.g. when selecting one option limits the possibilities for related fields. But I also hate having sloppy controllers.

What do others think? Is one approach better? Or is there an altogether different way of dealing with this?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.