How to relate one table with others using Eloquent ORM

和自甴很熟 提交于 2019-12-10 11:27:59

问题


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.


回答1:


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.




回答2:


This is the way you should be able to use them all:

$army = Army::find(1);

foreach($army->units as $unit)
{
    echo $unit->name;

    /// and you can

    echo $unit->army->name; /// too
}

And if you need to add a little more filter, you can

$army = Army::find(1);
$units = $army->units()->where('value','>',100)->get();

Note that you need to add '()' to use it as a query.



来源:https://stackoverflow.com/questions/16943446/how-to-relate-one-table-with-others-using-eloquent-orm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!