如何使用雄辩ORM将一张桌子与他人联系起来 [英] How to relate one table with others using Eloquent ORM

查看:84
本文介绍了如何使用雄辩ORM将一张桌子与他人联系起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题(获取外国人的名字关键的雄辩)我问了一些与Laravel 4雄辩的关系,但是当我进入一个更复杂的模式时,我开始越来越多地对如何正确的方式感到困惑。我想我可以简单地模仿一个常规的SQL查询,但是我想用正确的方法,使用模型和所有的工具,我有laravel和雄辩。



所以这些是我的桌子(其中一些)

 单位
+ ----------- - + ------------------ +
| Field |类型|
+ ------------ + ------------------ +
| id | int(10)unsigned |
|名称| varchar(255)|
|类型| int(11)|
| created_at |时间戳|
| updated_at |时间戳|
|值| int(11)|
|军队| int(11)|
+ ------------ + ------------------ +

军队
+ ------------ + ------------------ +
| Field |类型|
+ ------------ + ------------------ +
| id | int(10)unsigned |
|名称| varchar(255)|
| created_at |时间戳|
| updated_at |时间戳|
|颜色| varchar(255)|
+ ------------ + ------------------ +

Unittypes
+ ------------ + ------------------ +
| Field |类型|
+ ------------ + ------------------ +
| id | int(10)unsigned |
|名称| varchar(255)|
| created_at |时间戳|
| updated_at |时间戳|
+ ------------ + ------------------ +

您可以看到单位与军队和单位有1-n关系。我想要实现的是,当我在视图中列出我的单位时,而不是军队和单位的身份证件,我显示了实际的名字(也可能会在将来添加到这些表中的其他一些信息,我是在此之前,您可以在之前发布的链接中看到我已经使用hasMany或belongsTo方法设置了我的模型,这是我如何查询显示数据我不知道该怎么做。

解决方案

我认为我的答案你以前的问题也会回答这个问题。你不需要第三个表。在您的单位表中,最后一个字段的名称应为 army_id



修改



好的,您希望将单元格表与单位表关联起来。 单位表中的外键为类型,因此:



单位模型

  class Unit Enloquent 
{

public function army()
{
return $ this-> belongsTo('Army');
}

public function unittype()
{
return $ this-> belongsTo('Unittype','type');
}
}

单位模型应该看像这样:

  class Unittype扩展Eloquent 
{
public function units()
{
return $ this-> hasMany('Unit','type');
}
}

军队模型 p>

  class军队延伸Eloquent 
{
公共功能单位()
{
返回$这 - >的hasMany( '单元');
}

}

您的问题真的是渴望加载嵌套关系。因此,您的控制器(或路由)应该执行以下操作:

  $ armies = Army :: with('units.unittype') - > get(); 

最后,您的查看

 < ul> 
@foreach($ armies as $ army)
< li> {{$ army-> name}}
< ul>
@foreach($ army->单位为$ aunit)
< li>
< b> {{$ aunit-> name}}< / b>属于< b> {{$ aunit-> unittype-> name}}< / b>
< / li>
@endforeach
< / ul>
< / li>
@endforeach
< / ul>

免责声明:我会亲自合并单位和单位表,因为它是非常复杂的事情(从给出信息)。另请参阅您以前的问题和我的回答以充分了解发生了什么。


In a previous question (Getting the name of the foreign key with eloquent) I asked about one to many relationships with eloquent in Laravel 4 but as I get into a more complicated Schema I'm starting to get more and more confused about how todo it the proper way. I guess I can simply mimic a regular SQL query but I want to do this the proper way, using the models and all the tools I have with laravel and eloquent.

So these are my tables (some of them)

    Units
+------------+------------------+ 
| Field      | Type             | 
+------------+------------------+ 
| id         | int(10) unsigned | 
| name       | varchar(255)     | 
| type       | int(11)          | 
| created_at | timestamp        | 
| updated_at | timestamp        | 
| value      | int(11)          | 
| army       | int(11)          | 
+------------+------------------+ 

    Armies
+------------+------------------+
| Field      | Type             |
+------------+------------------+
| id         | int(10) unsigned |
| name       | varchar(255)     |
| created_at | timestamp        |
| updated_at | timestamp        |
| color      | varchar(255)     |
+------------+------------------+

    Unittypes
+------------+------------------+
| Field      | Type             |
+------------+------------------+
| id         | int(10) unsigned |
| name       | varchar(255)     |
| created_at | timestamp        |
| updated_at | timestamp        |
+------------+------------------+

As you can see units has 1-n relationship with armies and unittypes. What I want to achieve is, when I list my units on the view, that instead of the id of army and unittypes I show the actual name (and maybe some other info that would be added in the future to those tables, I'm doing the basics at the moment).

You can see in the link posted before that I've set up my models with the "hasMany" or "belongsTo" methods, it's how I do the query to show the data what I don't know how to do.

解决方案

I think my answer to your previous question will answer this one too. You don't need a third table. And in your units table, the name of the last field should be army_id

Edit

Ok, it seems you want to relate the unittypes table to the units table. Your foreign key on Units table is type, so:

Unit model:

class Unit extends Eloquent 
{

  public function army()
  {
     return $this->belongsTo('Army');
  }

  public function unittype()
  {
    return $this->belongsTo('Unittype', 'type');
  }
}

Unittype model should look like this:

class Unittype extends Eloquent 
{
  public function units()
  {
     return $this->hasMany('Unit', 'type');
  }
}

Army model

class Army extends Eloquent 
{
  public function units()
  {
     return $this->hasMany('Unit');
  }

}

Your problem really is eager-loading nested relationships. So, your controller (or route) should do:

$armies = Army::with('units.unittype')->get();

Lastly, your View

            <ul>
            @foreach($armies as $army)
                <li>{{$army->name}}
                   <ul>
                      @foreach($army->units as $aunit)
                        <li>        
                           <b>{{ $aunit->name }}</b> which belongs to <b>{{ $aunit->unittype->name }}</b>
                        </li>
                      @endforeach
                   </ul>
                 </li>
            @endforeach     
        </ul>   

Disclaimer: I would personally merge the units and unittypes table as it's complicating things for no reason (from the information given). Also, refer to your previous question and my answer in order to fully understand what's going on.

这篇关于如何使用雄辩ORM将一张桌子与他人联系起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆