Note that there are some explanatory texts on larger screens.

plurals
  1. POGet Object from Objects Laravel
    text
    copied!<p>i have two Models, first:</p> <pre><code>class Tutorial extends Eloquent { protected $table = 'tutorials'; public function rating() { return $this-&gt;hasMany('Rating'); } } </code></pre> <p>and:</p> <pre><code>class Rating extends Eloquent { protected $table = 'ratings'; public $timestamps = false; public function tutorial() { return $this-&gt;belongsTo('Tutorial'); } } </code></pre> <p>now in my controller i have this:</p> <pre><code>public function get_index() { $tutorials = tutorial::orderBy('created_at', 'desc') -&gt;with('rating') -&gt;paginate(25); return View::make('site/home/index')-&gt;with('tutorials', $tutorials); } </code></pre> <p>So how do i get all ratings from one tutorial in my View?!</p> <p>EDIT:</p> <p>Now i have this:</p> <pre><code>public function ratings() { return $this-&gt;hasMany('Rating'); } public function getRating() { // Grab the ratings from this tutorial $ratings = $this-&gt;ratings; $summedRatings = 0; // Loop through them all and add them together foreach($ratings as $rating) { console.log($rating-&gt;value); $summedRatings += $rating-&gt;value; } // Return the calculated average return $summedRatings / count($ratings); } public function get_index() { $tutorials = Tutorial::with('ratings') -&gt;with('user') -&gt;orderBy('created_at', 'desc') -&gt;paginate(25); return View::make('site/home/index')-&gt;with('tutorials', $tutorials); } </code></pre> <p>and in my View:</p> <pre><code>@foreach($tutorials as $tutorial) &lt;span&gt;{{$tutorial-&gt;rating}}&lt;/span&gt; @endforeach </code></pre> <p>But all my &lt; span >´s are empty! </p> <p>UPDATE: if i do this:</p> <pre><code>@foreach($tutorials as $tutorial) @foreach($tutorial-&gt;ratings as $rate) &lt;span&gt;{{$rate-&gt;value}}&lt;/span&gt; @endforeach </code></pre> <p>everything is good....So what´s wrong?</p>
 

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