laravel 用eloquent获取外键名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16937085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:53:28  来源:igfitidea点击:

Getting the name of the foreign key with eloquent

laravellaravel-4eloquent

提问by Elaine Marley

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):

因此,在我的单位表中,我有一行名为“军队”,其中包含与该单位相关的军队的 id,我想以以下方式在我的视图中显示一个简单的 ul 列表(我希望它显示在单位上索引视图):

 <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):

在我看来(index.blade.php 在视图/单位内):

<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?

但是 {{$unit->army}} 只是(当然)显示军队的 id,我想要名字,我该怎么做?

回答by kJamesy

Assuming you have set up your schema correctly: armiestable has at least id(auto-increment) and unitstable has at least id(auto-increment) and army_id(integer, unsigned), then, try this:

假设您已正确设置架构:armies表至少有id(自动递增),units表至少有id(自动递增)和Army_id(整数,无符号),然后,试试这个:

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 ControllerYou want to getall armies with units:

在您的控制器中,您希望获得所有带有单位的军队

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

In your Viewyou 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.

如果这不起作用,我会吃掉我的头。