【问题标题】:Get Object from Objects Laravel从 Laravel 对象中获取对象
【发布时间】:2013-08-11 14:40:14
【问题描述】:

我有两个模型,第一个:

class Tutorial extends Eloquent {

protected $table = 'tutorials'; 

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

}

和:

class Rating extends Eloquent {

protected $table = 'ratings';

public  $timestamps = false;

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

现在在我的控制器中我有这个:

public function get_index() {

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

    return View::make('site/home/index')->with('tutorials', $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)
    {
        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);
}

在我看来:

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

@endforeach

但是我所有的都是空的!

更新:如果我这样做:

@foreach($tutorials as $tutorial)

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

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

            @endforeach

一切都很好....那么有什么问题?

【问题讨论】:

    标签: javascript jquery laravel laravel-4


    【解决方案1】:

    首先,就命名约定而言,为了让事情更容易理解:教程方法中的rating() 方法应称为ratings(),因此当您获取评分时,它看起来会更好($tutorial-&gt;ratings )

    重命名后,在您看来,在循环访问 $tutorials 数组时,您可以像这样访问每个人的评分:

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

    这将检索每个的评级对象。

    您应该知道的是,如果您需要返回评分的计算结果,您可以为模型创建属性,而不是 ORM 对象

    例如,如果每个评分是存储在 amount 列中的评分表中从 1 到 5 的数字,您可以这样做将每个评分的平均值设置为一个属性:

    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);
    }
    
    }
    

    然后在您的视图中,您可以回显该属性,就好像它是数据库的一部分

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

    【讨论】:

    • 那行不通!给我调用未定义的方法 Illuminate\Database\Query\Builder::rating()
    • 这可能是因为我在 Laravel 3/4 之间跳转而忘记了骆驼肠衣在哪里。尝试将public function get_rating() 更改为public function getRating(),应该可以解决问题。
    • 它为什么会在 \Query\Builder 类上引发错误没有意义,因为它应该是一个 Tutorial 对象,因为它已经从数据库中检索出来......如果您更改了其他任何内容,您介意更新您的代码到现在的位置吗?
    【解决方案2】:

    根据您网站所在的平台,您应该始终使用正确的大小写。

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

    要急切加载评分,您应该始终先声明“with”方法。

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

    由于某种原因,这已被排除在 L4 文档之外。

    在您看来,您现在可以使用此访问评级

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

    【讨论】:

    • +1 用于正确的套管和急切的加载提示,我之前没有意识到该方法必须放在哪里......
    • 它给了我 500 内部服务器错误!
    • 那么你有Eloquent Tutorial 模型的评级属性吗? var 转储它。如果它不存在,那么您的关系未与您的数据库正确配置。
    • var dump 是什么意思?!
    • var_dump($tutorial)
    猜你喜欢
    • 2021-02-04
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 2022-01-24
    • 1970-01-01
    • 2018-05-15
    • 2014-01-22
    相关资源
    最近更新 更多