0

i have two Models, first:

class Tutorial extends Eloquent {

protected $table = 'tutorials'; 

public function rating()
{
    return $this->hasMany('Rating');
}   

}

and:

class Rating extends Eloquent {

protected $table = 'ratings';

public  $timestamps = false;

public function tutorial()
{
    return $this->belongsTo('Tutorial');
}   
}

now in my controller i have this:

public function get_index() {

    $tutorials = tutorial::orderBy('created_at', 'desc')
        ->with('rating')
        ->paginate(25);

    return View::make('site/home/index')->with('tutorials', $tutorials);
}

So how do i get all ratings from one tutorial in my View?!

EDIT:

Now i have this:

public function ratings()
{
    return $this->hasMany('Rating');
}   

public function getRating()
{
    // Grab the ratings from this tutorial
    $ratings = $this->ratings;
    $summedRatings = 0;

    // Loop through them all and add them together
    foreach($ratings as $rating)
    {
        console.log($rating->value);
        $summedRatings += $rating->value;
    }

    // Return the calculated average
    return $summedRatings / count($ratings);
}

public function get_index() {

    $tutorials = Tutorial::with('ratings')
        ->with('user')
        ->orderBy('created_at', 'desc')
        ->paginate(25);

    return View::make('site/home/index')->with('tutorials', $tutorials);
}

and in my View:

@foreach($tutorials as $tutorial)
<span>{{$tutorial->rating}}</span>

@endforeach

But all my < span >´s are empty!

UPDATE: if i do this:

@foreach($tutorials as $tutorial)

            @foreach($tutorial->ratings as $rate)

            <span>{{$rate->value}}</span>

            @endforeach

everything is good....So what´s wrong?

2 Answers 2

2

Depending on the platform you're site is on you should always use the correct case.

$tutorials = tutorial::orderBy(...) // Wrong

$tutorials = Tutorial::orderBy(...) // Correct

To eager load the ratings you should always declare your 'with' method before anything else.

$tutorials = Tutorial::with('rating')
                 ->orderBy('created_at', 'DESC')
                 ->paginate(25);

This has, for some reason, been left out of the L4 docs.

In your view you can now access the rating with this

foreach($tutorials as $tutorial)
{
    echo $tutorial->rating->{rating table column name};
}
Sign up to request clarification or add additional context in comments.

7 Comments

+1 for correct casing & the eager loading tip, I hadn't realised where that method must be placed before...
it gives me a 500 Internal Server Error!
well do you have the rating property of the Eloquent Tutorial model? var dump it. If it isn't there then you're relationships are not configured correctly with your DB.
What does it mean var dump it?!
var_dump($tutorial)
|
0

First, as far as naming conventions go, to make things easier to understand: The rating() method within your tutorial method should be called ratings(), so when you grab your ratings, it will look better ($tutorial->ratings)

After renaming this, in your view, while looping through the array of $tutorials, you could access the ratings of each one like this:

foreach($tutorials as $tutorial)
{
    $ratings = $tutorial->ratings;
}

Which would retrieve the ratings object of each.

What you should know is that you can create properties for your model if you need to return the calculation of the ratings, instead of the ORM objects

For example, if each rating is a number from 1-5 in the ratings table stored in an amount column, you can do this to set the average of each rating as a property:

class Tutorial extends Eloquent {

protected $table = 'tutorials'; 

public function ratings()
{
    return $this->hasMany('Rating');
}  

public function getRating()
{
    // Grab the ratings from this tutorial
    $ratings = $this->ratings;
    $summedRatings = 0;

    // Loop through them all and add them together
    foreach($ratings as $rating)
    {
        $summedRatings += $rating->amount;
    }

    // Return the calculated average
    return $summedRatings / count($ratings);
}

}

Then in your view, you can echo out the property as if it were part of the database

foreach($tutorials as $tutorial)
{
    echo $tutorial->rating;
}

3 Comments

that doesn´t work! Gives me Call to undefined method Illuminate\Database\Query\Builder::rating()
This may be due to me jumping between Laravel 3/4 and forgetting where camel casing is. Try change public function get_rating() to public function getRating(), and that should fix it.
It doesn't make sense as to why it is throwing an error on the \Query\Builder class, as it should be a Tutorial object since it has already been retrieved from the database... Would you mind updating your code as to where it is at now if you have changed anything else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.