将多行提取到单个集合对象中 - Laravel 4

md1*_*nox 1 optimization laravel laravel-4

这是我的控制器功能的样子:

public function index()
{
        $fixtures = Fixture::with('homeTeam', 'awayTeam')->get();
    $homeTeams=$fixtures->fetch('home_team');
    $awayTeams=$fixtures->fetch('away_team');
    return View::make('fixtures',array('home'=>$homeTeams,'away'=>$awayTeams));
}
Run Code Online (Sandbox Code Playgroud)

我的目标是在视图中打印团队:

Home      -      Away
India     -      Australia
Sri Lanka -      England
and so on......
Run Code Online (Sandbox Code Playgroud)

在我看来,我有:

@foreach($home as $homeT)
    <li>{{$homeT->tname}}</li>
@endforeach
Run Code Online (Sandbox Code Playgroud)

这会引发错误:

试图获得非对象的属性

如果我使用以下内容来查看$homeT包含的内容:

@foreach($home as $homeT)
    <li>{{$homeT}}</li>
@endforeach
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

数组到字符串转换

我的问题:如上所述,将两个行(homeTeam和awayTeams)发送到视图并打印它们的最佳方法是什么?

EDIT1

如果我使用以上foreach循环正确打印homeTeams:

<li> {{$homeT['tname'] </li>
Run Code Online (Sandbox Code Playgroud)

EDIT2

数据库结构(必需部分)

//Table Name : team
tid               PK
team_name         (varchar)
tname             (varchar)
team_details      (varchar)


//Table Name : fixture
fid               PK
rid               FK
lid               FK
home_team_id      FK   |_ both referenced to 'tid' from 'team' table
away_team_id      FK   |
date
venue
Run Code Online (Sandbox Code Playgroud)

这是模型:Fixture.php

class Fixture extends Eloquent {
    protected $table='fixture';
    protected $primaryKey = 'fid';

    public function homeTeam()
    {
        return $this->belongsTo('Team','home_team_id');
    }

    public function awayTeam()
    {
        return $this->belongsTo('Team','away_team_id');
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,在下面应用@ hayhorse的解决方案时,我会收到大量不需要的数据,即ID和场地等等.如果可以避免并且只能获取团队名称,那将是很好的,否则会有很多不需要的数据必须被发送到视图.

hay*_*rse 6

试试这个:

调节器

public function index()
{
    $fixtures = Fixture::with('homeTeam', 'awayTeam')->get();

    return View::make('fixtures', array('fixtures'=>$fixtures));
}
Run Code Online (Sandbox Code Playgroud)

视图

@foreach($fixtures as $fixture)
    <li>{{ $fixture->homeTeam->tname }} - {{ $fixture->awayTeam->tname }}</li>
@endforeach
Run Code Online (Sandbox Code Playgroud)