这是我目前陷入困境的地方。我正在执行多种方法,对同一个表进行不同的查询:
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 类似的东西。