Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple tables in one model - Laravel

My Index page uses 3 tables in the database:

  • index_slider
  • index_feature
  • footer_boxes

I use one controller (IndexController.php) and call the three models like so:

public function index() { 
return View::make('index')
->with('index_slider', IndexSlider::all())
->with('index_feature', IndexFeature::all())
->with('footer_boxes', FooterBoxes::all()); 
}

The three models above need ::all() data, so they are all setup like this:

class IndexSlider extends Eloquent {
public $table ='index_slider';
}

note: class name changes for each model

Seeing as my index page requires these 3 tables and the fact I am repeating the syntax in each model then should I be using polymorphic relations or setting this up differently? ORM from what I have read should have 1 model for each table, but I can't help but feel this would be silly in my situation and many others. DRY (don't repeat yourself) looses meaning in a sense.

What would be the best approach to take here or am I on the right track?

like image 816
Panoply Avatar asked Dec 20 '22 17:12

Panoply


1 Answers

Firstly I should say each model is written for a specific table, you can't squeeze three tables into one model unless they are related. See Here

There are two ways I would go about making your code more DRY. Instead of passing your data in a chain of withs I would pass it as the second parameter in your make:

public function index() { 
    $data = array(
        'index_slider'  => IndexSlider::all(),
        'index_feature' => IndexFeature::all(),
        'footer_boxes'  => FooterBoxes::all(),
    );

    return View::make('index', $data);
}

Passing data as the second parameter. See here

The other way I would go about it, and this is a better solution if your application is going to grow large, is to create a service (another model class but not hooked up to eloquent) that when you call will return the necessary data. I would definitely do it this way if you are returning the above data in multiple views.

An example of using a service would look something like this:

<?php 
// app/models/services/indexService.php
namespace Services;

use IndexSlider;
use IndexFeature;
use FooterBoxes;

class IndexService
{
    public function indexData()
    {
        $data = array(
            'index_slider'  => IndexSlider::all(),
            'index_feature' => IndexFeature::all(),
            'footer_boxes'  => FooterBoxes::all(),
        );

        return $data;
    }
}

and your controller:

<?php
// app/controllers/IndexController.php

use Services/IndexService;

class IndexController extends BaseController
{
    public function index() { 
        return View::make('index', with(new IndexService())->indexData());
    }
}

This service can be expanded with a lot less specific methods and you should definitely change the naming (from IndexService and indexData to more specific class/method names).

If you want more information on using Services I wrote a cool article about it here

Hope this helps!

like image 126
Chris Goosey Avatar answered Dec 24 '22 03:12

Chris Goosey