Laravel 9 未定义变量 $variable

Laravel 9 Undefined variable $variable

我在 if() 函数中定义了 2 个变量 $b$c,但是当 运行 函数时,我保留获得 Undefined variable $b.

public function printPDF()
    {
        /*Treadwear*/
        if ($this->tread_wear == 140) {
            $c = 140;
        } elseif ($this->tread_wear == 0) {
            $b = 120;
        } elseif ($this->tread_wear < 50) {
            $b = 80;
        } elseif ($this->tread_wear < 140) {
            $b = 40;
        } elseif ($this->tread_wear < 201) {
            $b = 20;
        }

        $pdf = new Pdf('/docs/ax_reg_form.pdf');
        $result = $pdf->fillForm([
            ...
            'b' => $b,
            'c' => $c,
            ...
        ])
            ->needAppearances()
            ->saveAs('docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');

        // Always check for errors
        /*if ($result === false) {
            $error = $pdf->getError();
        }*/

        session()->flash('url', '/docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');

        return redirect()->route('usercar.show', $this->usercar->id);

    }

您对 $b$c 的声明在 ifelseif 中,因此只会声明其中一个。也许你忘记了 else 语句?