【问题标题】:Laravel Object Relation does not work in foreach loopLaravel 对象关系在 foreach 循环中不起作用
【发布时间】:2014-06-11 12:13:28
【问题描述】:

我有一个典型的模型关系。我有模型QRhasMany Rating,还有模型RatingbelongsTo Qr

现在我想通过这样的foreach 循环输出属于单个qr 模型的Ratings

            <table>
                <tr>
                    <th>ID</th>
                    <th>UnitID</th>
                    <th># of Ratings</th>
                </tr>

                @foreach($qrs as $qr->ratings)

                <tr>
                    <td>{{$qr->id}}</td>
                    <td>{{$qr->unit_id}}</td>
                    <td>{{$qr->ratings->count()}}</td>
                </tr>

                @endforeach
            </table>

这是我的控制器:

public function index()
{
    //
    $unit = Unit::all()->first();
    $qrs = Qr::all()->first();
    return View::make('index')
        ->with('unit', $unit)
        ->with('qrs', $qrs);
}

这是我的两个模型

Rating.php:

    class Rating extends \Eloquent {
    protected $guarded = [];

    public function qr(){
        return $this->belongsTo('Qr');
    }
}

Qr.php:

    class Qr extends \Eloquent {
    protected $guarded = [];

    public function unit(){
        return $this->belongsTo('Unit');
    }

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

我实际上想输出ratings 的计数,这是一个二维码。我知道可以这样做:

 {{Rating::where('qr_id', $qr->id)->count()}}

但我想在 foreach 循环中以某种方式这样做

 {{ $Qr->rating->count() }}

如果这是可能的。

我得到了关系,如果我只输出Qrfirst() 然后 var_dump($qrs->ratings->toArray())

但我不知道如何结合foreach 循环来获得评分数。任何帮助将不胜感激。

【问题讨论】:

    标签: php html oop laravel eloquent


    【解决方案1】:

    这里有几个问题:

    // view:
    @foreach($qrs as $qr->rating) 
    // should be: 
    @foreach($qrs as $qr)
    
    // controller:
    $unit = Unit::all()->first();
    $qrs = Qr::all()->first();
    // this way you get all Units then fetch first Unit from the collection,
    // the same with Qrs, so change it to:
    $unit = Unit::all(); // do you need it at all?
    $qrs = Qr::with('ratings')->get();
    

    这将解决问题,并且在 foreach 循环中您将能够访问 $qr->ratings->count() 这将是 Collection 方法。

    【讨论】:

    • 谢谢。将在几个小时内尝试...我现在正在路上,无法测试它。但绝对感谢您的贡献。
    【解决方案2】:

    一开始你是这样用的:

    @foreach($qrs as $qr->ratings)
    

    您需要将其更改为此(如答案中所述):

    @foreach($qrs as $qr)
    

    然后在你的index 方法中你使用了这个:

    public function index()
    {
        $unit = Unit::all()->first();
        $qrs = Qr::all()->first();
        return View::make('index')->with('unit', $unit)->with('qrs', $qrs);
    }
    

    在这种情况下,您需要获取QR 模型的集合,并且由于UnitRatingQr 相关,那么您可以使用withget() 来获取@987654331 的集合@这样的模型:

    public function index()
    {
        $qrs = Qr::with(array('unit', 'ratings'))->get();
        return View::make('index')->with('qrs', $qrs);
    }
    

    然后您就可以像这样在您的view 中循环Qr 模型:

    @foreach($qrs as $qr)
        <tr>
            <td>{{ $qr->id }}</td>
            <td>{{ $qr->unit_id }}</td>
            <td>{{ $qr->ratings->count() }}</td>
        </tr>
    @endforeach
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2019-06-28
      • 2013-11-14
      • 2020-05-27
      • 2014-04-28
      相关资源
      最近更新 更多