Reputation: 2213
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.
Upvotes: 2
Views: 4073
Reputation: 6233
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.
Upvotes: 6
Reputation: 87719
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.
Upvotes: 0