Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从对象Laravel获取对象_Javascript_Jquery_Laravel_Laravel 4 - Fatal编程技术网

Javascript 从对象Laravel获取对象

Javascript 从对象Laravel获取对象,javascript,jquery,laravel,laravel-4,Javascript,Jquery,Laravel,Laravel 4,我有两种型号,第一种: class Tutorial extends Eloquent { protected $table = 'tutorials'; public function rating() { return $this->hasMany('Rating'); } } 以及: 现在,在我的控制器中,我有: public function get_index() { $tutorials = tutorial::orderBy('created

我有两种型号,第一种:

class Tutorial extends Eloquent {

protected $table = 'tutorials'; 

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

}
以及:

现在,在我的控制器中,我有:

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);
}
那么,在我看来,如何从一个教程中获得所有评分

编辑:

现在我有这个:

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作为$tutorial)
{{$tutorial->rating}
@endforeach
但是我所有的都是空的

更新:如果我这样做:

@foreach($tutorials as $tutorial)

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

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

            @endforeach
@foreach($tutorials作为$tutorial)
@foreach($tutorial->评级为$rate)
{{$rate->value}
@endforeach

一切都很好……那么怎么了?

根据您的站点所在的平台,您应该始终使用正确的案例

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

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

重命名后,在您的视图中,在循环浏览$tutorials数组时,您可以访问每个教程的评级,如下所示:

foreach($tutorials as $tutorial)
{
    $ratings = $tutorial->ratings;
}
它将检索每个的ratings对象

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

例如,如果每个评级是存储在
金额
列中的评级表中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;
}

+1对于正确的外壳和急切的加载提示,我以前没有意识到该方法必须放置在何处…它给了我500个内部服务器错误!那么,您是否拥有
雄辩教程
模型的评级属性?我把它扔了。如果它不存在,那么您的关系没有正确配置数据库。这意味着什么<代码>变量转储($tutorial)不起作用!调用未定义的方法illumb\Database\Query\Builder::rating(),这可能是因为我在Laravel 3/4之间跳转,忘记了骆驼壳的位置。尝试将
公共函数get_rating()
更改为
公共函数getRating()
,这应该可以解决它。它在\Query\Builder类上抛出错误的原因是没有意义的,因为它应该是教程对象,因为它已经从数据库中检索到了。。。如果您更改了其他内容,您是否介意更新您的代码以了解它现在的位置?