用雄辩的方式获得外键的名字 [英] Getting the name of the foreign key with eloquent
问题描述
这是一个非常基本的东西,但我似乎无法找出如何做到这一点。
我与表之间有一对多的关系:单位和军队(军队包含许多单位),这些是我的模型:
类军队延伸口才{
protected $ guarded = array();
public static $ armies = array();
protected $ table ='armies';
public function units()
{
return $ this-> hasMany('Unit');
}
}
和:
class Unit Eloquent {
protected $ guarded = array();
public static $ units = array();
protected $ table ='units';
public function armies()
{
return $ this-> belongsTo('Army','army');
}
}
所以,我的单位表我有一个名为军队的行,其中包含与该单位相关的军队的身份,我想以我的观点显示一个简单的ul列表,以下列方式(我希望在单位索引视图中显示) :
< ul>
< li>军队1名称
< li>军队1的单位1< / li>
< li>军1的单位2< / li>
< / li>
< li>军队2名称
< li>军队2的单位1< / li>
< li>军队2的单位2< / li>
< / li>
< / ul>
要做到这一点,我的单位控制器如下所示:
class UnitsController extends BaseController {
protected $ layout ='master';
/ **
*显示资源列表。
*
* @return响应
* /
public function index()
{
$ units = Unit :: all();
$ this-> layout-> content = View :: make('units.index',compact('units'));
}
/ *其他东西* /
}
在我看来(index.blade.php里面的视图/单位):
< ul类= 单位 >
@foreach($ units as $ unit)
{{$ unit-> army}}
< li>
{{HTML :: link(route('units.show',['units'=> $ unit-> id]),$ unit-> name)}}
< /立GT;
@endforeach
< / ul>
但{{$ unit-> army}}只是(当然)显示军队,我想要这个名字,我该怎么做?
假设你已经正确设置了你的架构: armies 表至少具有 id (自动递增)和单位表至少包含 id (自动递增)和 army_id (整数,未签名),然后尝试:
模型
class军队延伸口才{
公共功能单位()
{
返回$ this-> hasMany('Unit' );
}
}
class单元扩展Eloquent {
public function armies()
{
return $ this-> ;属于关联( '军');
}
}
在您的控制器中
您想要获得所有单位的军队:
$ armies = Army :: with('units') - > get();
在您的视图中,您想循环输出名称的结果集每个军队及其各自的单位
@foreach($ armies as $ army)
{{$ army-> ; name}}
< ul>
@foreach($ army->单位为$ aunit)
< li>
{{$ aunit-> name}}
< / li>
@endforeach
< / ul>
@endforeach
如果不行,我会吃我的头。 / p>
This must be very basic stuff but I can't seem to find out how to do it.
I have a one to many relationship between to tables: Unit and Army (an army contains many units) and these are my models:
class Army extends Eloquent {
protected $guarded = array();
public static $armies = array();
protected $table = 'armies';
public function units()
{
return $this->hasMany('Unit');
}
}
and:
class Unit extends Eloquent {
protected $guarded = array();
public static $units = array();
protected $table = 'units';
public function armies()
{
return $this->belongsTo('Army', 'army');
}
}
So, in my Unit table I have a row called"army" that contains the id of the army related to that unit and I want to show in my view a simple ul list in the following fashion (I want this to show on the unit index view):
<ul>
<li>Army 1 name
<li>Unit 1 of Army 1</li>
<li>Unit 2 of Army 1</li>
</li>
<li>Army 2 name
<li>Unit 1 of Army 2</li>
<li>Unit 2 of Army 2</li>
</li>
</ul>
To do this I have my unit controller like so:
class UnitsController extends BaseController {
protected $layout = 'master';
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$units = Unit::all();
$this->layout->content = View::make('units.index', compact('units'));
}
/*Other stuff*/
}
And in my view (index.blade.php inside views/units):
<ul class="units">
@foreach($units as $unit)
{{$unit->army}}
<li>
{{ HTML::link(route('units.show', ['units' => $unit->id]), $unit->name)}}
</li>
@endforeach
</ul>
But {{$unit->army}} is just (of course) showing the id of the army, and I want the name, how do I do this?
Assuming you have set up your schema correctly: armies table has at least id(auto-increment) and units table has at least id(auto-increment) and army_id (integer, unsigned), then, try this:
Models
class Army extends Eloquent {
public function units()
{
return $this->hasMany('Unit');
}
}
class Unit extends Eloquent {
public function armies()
{
return $this->belongsTo('Army');
}
}
In your Controller You want to get all armies with units:
$armies = Army::with('units')->get();
In your View you want to loop through the result set outputting the name of each army and its respective units
@foreach($armies as $army)
{{$army->name}}
<ul>
@foreach($army->units as $aunit)
<li>
{{ $aunit->name }}
</li>
@endforeach
</ul>
@endforeach
If that doesn't work, I'll eat my head.
这篇关于用雄辩的方式获得外键的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!