【发布时间】:2017-10-02 18:51:45
【问题描述】:
这是我目前陷入困境的地方。我正在使用多种方法对同一个表进行不同的查询:
public function totalOfA()
{
return $a = Stocks::where('user_id','=', $this->employee->id)
->where('category','=','a')
->pluck('qty')
->sum();
}
public function totalOfB()
{
return $a = Stocks::where('user_id','=', $this->employee->id)
->where('category','=','a')
->pluck('qty')
->sum();
}
我正在尝试寻找一种方法将所有这些总结为一个函数
public function totalStocks()
{
$stocks = Stocks::where('user_id','=', $this->employee->id)
->get();
$a = $stocks::where('category', '=', 'a')
->pluck('qty')
->sum();
$b = $stocks::where('category', '=', 'b')
->pluck('qty')
->sum();
return $a and $b
}
所以我可以从视图中将其称为 totalstocks()->a 或 totalstocks()->b 之类的。
【问题讨论】:
-
您对第二个代码块有疑问吗?
-
你为什么不使用范围