Laravel - 在一个方法中返回多个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46518640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Return multiple values within one method
提问by Oliver Vincent Ledesma
Heres where I'm currently stuck at. I am doing multiple methods where I am doing the different queries to the same table:
这就是我目前卡在的地方。我正在执行多种方法,对同一个表执行不同的查询:
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();
}
I'm trying to look for a way to sum that all up to just one function
我正在尝试寻找一种方法来将所有内容总结为一个功能
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
}
So I can just call it from view as totalstocks()->a or totalstocks()->b something like that.
所以我可以从视图中调用它作为 totalstocks()->a 或 totalstocks()->b 类似的东西。
回答by iCoders
You cant call like that.you can create an aray or you can pass seperately to view
你不能那样调用。你可以创建一个数组,也可以单独传递查看
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]);
}
Now you can access $a ,$b in your view
现在您可以在视图中访问 $a ,$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]);
}
so you can accessin view like {{$data['a']}}
所以你可以像这样访问视图 {{$data['a']}}
回答by HoangNK
Try this:
尝试这个:
$x = array(
'a' => $a,
'b' => $b,
);
return (object) $x
回答by Pubudu Jayawardana
Possible solution is that you define private/protected variables as a
and b
within the class and in the method totalStocks()
you can set values to them.
可能的解决方案是您在类中定义私有/受保护变量,a
并b
在方法中totalStocks()
为它们设置值。
So your function would look like this:
所以你的函数看起来像这样:
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;
}
So you can access $this->a
and $this->b
as properties of the class whenever class is in use.
因此,无论何时使用类,您都可以访问$this->a
和$this->b
作为类的属性。
回答by Link.de
try this:
尝试这个:
function totalStocks() {
$a = 1;
$b = 2;
return [$a, $b];
}
list($valueA, $valueB) = totalStocks();
echo $valueA; //1
echo $valueB; //2