简体   繁体   English

一个模型中的多个表 - Laravel

[英]Multiple tables in one model - Laravel

My Index page uses 3 tables in the database: 我的索引页面在数据库中使用3个表:

  • index_slider index_slider
  • index_feature index_feature
  • footer_boxes footer_boxes

I use one controller (IndexController.php) and call the three models like so: 我使用一个控制器(IndexController.php)并调用三个模型,如下所示:

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: 上面的三个模型需要:: all()数据,因此它们都是这样设置的:

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? 看到我的索引页面需要这3个表,而事实是我在每个模型中重复语法,那么我应该使用多态关系还是以不同的方式设置它? 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. 我读过的ORM应该有每个表的1个模型,但我不禁觉得这对我的情况和其他许多人来说都是愚蠢的。 DRY (don't repeat yourself) looses meaning in a sense. DRY(不要重复自己)在某种意义上失去意义。

What would be the best approach to take here or am I on the right track? 什么是最好的方法来到这里或我是在正确的轨道上?

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: 我不会将数据传递给withs链中,而是将其作为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). 可以使用更少的特定方法扩展此服务,您应该更改命名(从IndexService和indexData到更具体的类/方法名称)。

If you want more information on using Services I wrote a cool article about it here 如果您想了解有关使用服务的更多信息,我在这里写了一篇很酷的文章

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM