2015-10-23 48 views
2

我在一些地方在我的控制器中使用查询范围,但现在我试图在我的刀片模板中过滤掉一些结果。我的模型有一个有订单的位置。我通过定位到刀片模板,然后使用以下命令:使用查询范围过滤Laravel Blade模板中的结果

@foreach($location->orders->datesearch('delivered_at','2015-10-01','2015-10-15') as $order) 
    {{ $order->order_number }}<br /> 
@endforeach 

下面是摘录从我的订货型号

class Order extends Model { 

    public function location() 
    { 
     return $this->belongsTo('App\Location', 'location_id'); 
    } 

    public function scopeDatesearch($query,$datesearch,$search_date_start,$search_date_end) 
     { 
      return $query->whereBetween($datesearch, [$search_date_start, $search_date_end])->get(); 
     } 
} 

位置的hasMany订单也是如此。

我得到的错误是:

FatalErrorException in 56b3a0ff15f6ddc28458248fd0728a27 line 5: 
Call to undefined method Illuminate\Database\Eloquent\Collection::datesearch() 

我发现this similar posting这似乎表明我在做什么应该工作。

回答

2

从您的错误消息中,您可以看到$ location-> orders是一个集合,但范围不在集合中使用。 尝试改变你的刀片是这样的:

@foreach($location->orders()->datesearch('delivered_at','2015-10-01','2015-10-15')->get() as $order) 
    {{ $order->order_number }}<br /> 
@endforeach 
+1

这与从我的范围中删除 - > get()解决了问题。谢谢雷。 –