是否可以在模型内创建自定义查询构建器并返回查询?这不是一个压力大的问题,但会有所帮助。
/* Controller */
public function getOrders()
{
$orders = \App\Order::where('is_new', 1)->getFromUserStore();
}
/* Order Model */
public function getFromUserStore()
{
if(\Auth::user()->store->id == 1)
{
return $this->get();
}
else
{
return $this->where('status_id', 1)->get();
}
}
谢谢
我相信您正在寻找查询范围
范围使您可以定义常见的约束集,您可以在整个应用程序中轻松地重用这些约束集。
/* Controller */
public function getOrders()
{
$orders = \App\Order::where('is_new', 1)->getFromUserStore()->get();
}
/* Order Model */
public function scopeGetFromUserStore($query)
{
if(\Auth::user()->store->id == 1)
{
return $query;
}
else
{
return $query->where('status_id', 1);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句