Note that there are some explanatory texts on larger screens.

plurals
  1. POFetching multiple rows into single collection object - Laravel 4
    primarykey
    data
    text
    <p>Here is how my controller function looks like:</p> <pre><code>public function index() { $fixtures = Fixture::with('homeTeam', 'awayTeam')-&gt;get(); $homeTeams=$fixtures-&gt;fetch('home_team'); $awayTeams=$fixtures-&gt;fetch('away_team'); return View::make('fixtures',array('home'=&gt;$homeTeams,'away'=&gt;$awayTeams)); } </code></pre> <p>My aim is to print teams in the view as:</p> <pre><code>Home - Away India - Australia Sri Lanka - England and so on...... </code></pre> <p>In my View I have:</p> <pre><code>@foreach($home as $homeT) &lt;li&gt;{{$homeT-&gt;tname}}&lt;/li&gt; @endforeach </code></pre> <p>which throws an error:</p> <blockquote> <p>Trying to get property of non-object</p> </blockquote> <p>while if I use following to see what <code>$homeT</code> contains:</p> <pre><code>@foreach($home as $homeT) &lt;li&gt;{{$homeT}}&lt;/li&gt; @endforeach </code></pre> <p>I get following error:</p> <blockquote> <p>Array to string conversion</p> </blockquote> <p>My question(s): What is the best way to send both rows (homeTeam and awayTeams) to the view and print them as I've mentioned above?</p> <h2>Edit1</h2> <p>Above foreach loop prints homeTeams properly if I use:</p> <pre><code>&lt;li&gt; {{$homeT['tname'] &lt;/li&gt; </code></pre> <h2>Edit2</h2> <p>the database structure (required part of it)</p> <pre><code>//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 </code></pre> <p>here is the model : <strong>Fixture.php</strong></p> <pre><code>class Fixture extends Eloquent { protected $table='fixture'; protected $primaryKey = 'fid'; public function homeTeam() { return $this-&gt;belongsTo('Team','home_team_id'); } public function awayTeam() { return $this-&gt;belongsTo('Team','away_team_id'); } } </code></pre> <p>Thus, on applying @hayhorse's solution below, I get whole lot of unwanted data, viz, id's and venue etc etc. It'd be great if that could be avoided and only the team names could be fetched, otherwise so much of unwanted data has to be sent to the view.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload