在我目前被困的地方。我正在做多种方法,我正在对同一个表执行不同的查询:
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之类的东西。
答案 0 :(得分:1)
你不能这样打电话。你可以创建一个aray,或者你可以单独传递来查看
# This class contains the x and y values for a grid
class MapTile:
def __init__(self, x, y):
self.x = x
self.y = y
# basic char class containing position
class Character:
def __init__(self, name, hp, x, y):
self.name = name
self.hp = hp
self.x = x
self.y = y
# movement func
def Move(self, Direction):
if Direction.upper() == "UP":
if self.y > 1:
self.y -= 1
elif Direction.upper() == "LEFT":
if self.x > 1:
self.x -= 1
elif Direction.upper() == "RIGHT":
if self.x < 10:
self.x += 1
elif Direction.upper() == "DOWN":
if self.y < 10:
self.y += 1
def __str__(self):
return "{}\n========\nHP = {}\nX = {}\nY = {}".format(self.name,
self.hp,
self.x,
self.y)
现在您可以在视图中访问$ a,$ b
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 view('home',['stocks'=> $stocks,'a'=>$a,'b'=>$b]);
}
因此您可以访问 public function totalStocks()
{
$data['stocks'] = Stocks::where('user_id','=', $this->employee->id)
->get();
$data['a'] = $stocks::where('category', '=', 'a')
->pluck('qty')
->sum();
$data['b'] = $stocks::where('category', '=', 'b')
->pluck('qty')
->sum();
return view('home',['data'=>$data]);
}
答案 1 :(得分:0)
试试这个:
$x = array(
'a' => $a,
'b' => $b,
);
return (object) $x
答案 2 :(得分:0)
可能的解决方案是在类中定义私有/受保护变量为a
和b
,在方法totalStocks()
中可以为它们设置值。
所以你的功能看起来像这样:
public function totalStocks()
{
$stocks = Stocks::where('user_id','=', $this->employee->id)
->get();
$this->a = $stocks::where('category', '=', 'a')
->pluck('qty')
->sum();
$this->b = $stocks::where('category', '=', 'b')
->pluck('qty')
->sum();
// Return optional
return $this;
}
因此,无论何时使用类,您都可以访问$this->a
和$this->b
作为类的属性。
答案 3 :(得分:0)
试试这个:
function totalStocks() {
$a = 1;
$b = 2;
return [$a, $b];
}
list($valueA, $valueB) = totalStocks();
echo $valueA; //1
echo $valueB; //2