use QueryScope in Blade template

拥有回忆 提交于 2019-12-25 03:02:08

问题


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:


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!



来源:https://stackoverflow.com/questions/32700283/use-queryscope-in-blade-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!