Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义查询生成器Laravel_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 自定义查询生成器Laravel

Php 自定义查询生成器Laravel,php,laravel,laravel-5,Php,Laravel,Laravel 5,是否可以在模型内创建自定义查询生成器并返回查询?这不是一个压力大的问题,但会有帮助 /* Controller */ public function getOrders() { $orders = \App\Order::where('is_new', 1)->getFromUserStore(); } /* Order Model */ public function getFromUserStore() { if(\Auth::user()->store-&g

是否可以在模型内创建自定义查询生成器并返回查询?这不是一个压力大的问题,但会有帮助

/* 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();
  }
}

谢谢

我相信您正在寻找

作用域允许您定义可能需要的公共约束集 在整个应用程序中轻松重复使用


这是问题本身的一部分,但我建议您对名称空间使用
use
操作符,以避免一直使用反斜杠。对于您的控制器,您应该
使用App\Order并且对于您的模型
使用照明\Support\Facades\Auth
@Wistar,我通常使用
use
。。使用反斜杠是否存在性能问题?谢谢你的意见。我不知道性能问题。还有,答案有效吗?正是我想要的。谢谢您。
/* 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);
  }
}