我的控制器中有两个函数,但是当 运行 admin/panel 显示错误 Undefined variable: labels
I have two functions in my controller but when run admin/panel show error Undefined variable: labels
我有这个错误:
ErrorException Undefined variable: labels
getLastMonths()
方法中的return$labels
怎么办?
面板控制器
public function index()
{
$month=12;
$peymentSuccess=Payment::SpanningPayment($month,true);
$peymentunSuccess=Payment::SpanningPayment($month,false);
$labels = $this->getLastMonths($month);// mahe shamsi bar migardoune nasbe jalali morilog
$values['success']=$peymentSuccess->pluck('published');
$values['unsuccess']=$peymentunSuccess->pluck('published');
return view('admin.panel', compact('labels','values'));
}
private function getLastMonths( $month)
{
for ($i=0 ; $i>$month ; $i++)
{
$labels[]=jdate(Carbon::now()->subMonths($i))->format('%B');
}
return $labels;
}
您的代码:
$month=12;
// ...
$labels = $this->getLastMonths($month);
然后:
private function getLastMonths($month)
{
for ($i=0 ; $i>$month ; $i++)
// ...
这个循环永远不会工作 - $month
是 12,$i
是 0,0 永远不会大于 12。不会有迭代,$labels
永远不会设置,并且getLastMonths()
returns 没有。
您需要:
for ($i=0 ; $i < $month ; $i++)
或者(根据您的要求,我不确定您到底在做什么):
for ($i=0 ; $i <= $month ; $i++)
我认为您没有在 getLastMonths($month) 函数中初始化 $labels。
使用以下代码。
private function getLastMonths( $month)
{
$labels = [];
for ($i=0 ; $i<$month ; $i++)
{
$labels[]=jdate(Carbon::now()->subMonths($i))->format('%B');
}
return $labels;
}
我有这个错误:
ErrorException Undefined variable: labels
getLastMonths()
方法中的return$labels
怎么办?
面板控制器
public function index()
{
$month=12;
$peymentSuccess=Payment::SpanningPayment($month,true);
$peymentunSuccess=Payment::SpanningPayment($month,false);
$labels = $this->getLastMonths($month);// mahe shamsi bar migardoune nasbe jalali morilog
$values['success']=$peymentSuccess->pluck('published');
$values['unsuccess']=$peymentunSuccess->pluck('published');
return view('admin.panel', compact('labels','values'));
}
private function getLastMonths( $month)
{
for ($i=0 ; $i>$month ; $i++)
{
$labels[]=jdate(Carbon::now()->subMonths($i))->format('%B');
}
return $labels;
}
您的代码:
$month=12;
// ...
$labels = $this->getLastMonths($month);
然后:
private function getLastMonths($month)
{
for ($i=0 ; $i>$month ; $i++)
// ...
这个循环永远不会工作 - $month
是 12,$i
是 0,0 永远不会大于 12。不会有迭代,$labels
永远不会设置,并且getLastMonths()
returns 没有。
您需要:
for ($i=0 ; $i < $month ; $i++)
或者(根据您的要求,我不确定您到底在做什么):
for ($i=0 ; $i <= $month ; $i++)
我认为您没有在 getLastMonths($month) 函数中初始化 $labels。 使用以下代码。
private function getLastMonths( $month)
{
$labels = [];
for ($i=0 ; $i<$month ; $i++)
{
$labels[]=jdate(Carbon::now()->subMonths($i))->format('%B');
}
return $labels;
}