Home > Software design >  Laravel 9 Undefined variable $variable
Laravel 9 Undefined variable $variable

Time:04-11

I have 2 variables $b and $c defined in an if() function, but when running the function, I keep getting an 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);

    }

CodePudding user response:

Your declarations of $b and $c are inside if and elseif so only one of them will be declared. Maybe you forget about else statement?

  • Related