我的控制器中有两个功能,但在运行admin / panel时显示错误未定义变量:标签

问题描述 投票:0回答:1

我有此错误:

ErrorException未定义变量:标签

我应该怎么做才能以$labels方法返回getLastMonths()

panelcontroller

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;
    }
php laravel laravel-5.8
1个回答
0
投票

您的代码可以:

$month=12;
// ...
$labels = $this->getLastMonths($month);

然后:

private function getLastMonths($month)
{
    for ($i=0 ; $i>$month ; $i++)
    // ...

此循环将永远无法工作-$month为12,$i为0,0永远不会大于12。将不会有任何迭代,$labels也不会设置,并且getLastMonths()不返回任何内容。

您需要:

for ($i=0 ; $i < $month ; $i++)

或者(根据您的要求,我不确定您在做什么):

for ($i=0 ; $i <= $month ; $i++)

0
投票

我认为您没有在getLastMonths($ month)函数中初始化$ label。使用以下代码。

`private function getLastMonths( $month)
    {
        $labels = [];
        for ($i=0 ; $i>$month ; $i++)
        {
            $labels[]=jdate(Carbon::now()->subMonths($i))->format('%B');
        }
        return $labels;
    }`
© www.soinside.com 2019 - 2024. All rights reserved.