use QueryScope in Blade template

2019-09-08 10:33发布

I have define scope in model like this

class Station extends Model {

    protected $primaryKey = 'st_id';

    public function scopeByDid($query)
    {
        return $query->groupBy("st_did");
    }

}

I can call byDid from controller but I cannot get it through blade template like this

@foreach ($river->stations->byDid as $didType)
....
@endforeach

how do I get it. Appreciate your response. Thanks

1条回答
冷血范
2楼-- · 2019-09-08 11:11

If you're getting a relationship as an attribute (without () at the end) it means the relationship will have already been retrieved before the scope.

To get your code to work you will just need to change your foreach to:

@foreach($river->stations()->byDid()->get() as $didType)

Hope this helps!

查看更多
登录 后发表回答