猿问

我的控制器中有两个函数,但在运行管理/面板时显示错误 Undefined variable:

我有这个错误:


ErrorException 未定义的变量:标签


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


面板控制器

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;

    }


斯蒂芬大帝
浏览 129回答 2
2回答

智慧大石

您的代码执行以下操作:$month=12;// ...$labels = $this->getLastMonths($month);接着:private function getLastMonths($month){&nbsp; &nbsp; for ($i=0 ; $i>$month ; $i++)&nbsp; &nbsp; // ...这个循环永远不会工作 -$month是 12,$i是 0,0 永远不会大于 12。不会有迭代,$labels永远不会设置,并且getLastMonths()什么都不返回。你需要:for ($i=0 ; $i < $month ; $i++)或者也许(根据您的要求,我不确定您到底在做什么):for ($i=0 ; $i <= $month ; $i++)

皈依舞

我认为您没有在 getLastMonths($month) 函数中初始化 $labels 。使用以下代码。private function getLastMonths( $month)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $labels = [];&nbsp; &nbsp; &nbsp; &nbsp; for ($i=0 ; $i<$month ; $i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $labels[]=jdate(Carbon::now()->subMonths($i))->format('%B');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $labels;&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答