LaCor
LaCor

Reputation: 25

PHP/Laravel 9 Call to undefined method

I have a problem with my check function for data validation and i get the error

Call to undefined method App\Models\Product::check()

I use the Laravel framework for programming.

These are the methods, that i use for storing and updating my data.

public function store(Product $product)
    {
        Product::create($product->check())->save();

        return redirect('/');
    }

public function update(Product $product)
    {
        $product->update($this->check());

        return redirect('/');
    }

This is my validation Function. all are in the same controller.

protected function check()
    {
        if (request()->Produkt == 'Kuhli' || request()->Produkt == 'Warmi')
        {
            switch (request()->Produkt) {
                case 'Kuhli':
                    $data = Validation::where('id', '=', '1')->get();
                    $PHmax = $data[0]->PHmax;
                    $PHmin = $data[0]->PHmin;
                    $Wassermax = $data[0]->Wassermax;
                    $Wassermin = $data[0]->Wassermin;
                    $Dichtemax = $data[0]->Dichtemax;
                    $Dichtemin = $data[0]->Dichtemin;
                    return request()->validate([
                        'LieferNr' => ['required', 'min:5', 'max:5'],
                        'Produkt' => 'required',
                        'PH' => ['required', 'numeric', "min:$PHmin", "max:$PHmax"],
                        'Wasser' => "required|numeric|min:$Wassermin|max:$Wassermax",
                        'Dichte' => "required|numeric|min:$Dichtemin|max:$Dichtemax",
                        'Bearbeiter' => 'required',
                    ]);
                case 'Warmi':
                    $data = Validation::where('Produkt', '=', 'Warmi')->get();
                    $PHmax = $data[0]->PHmax;
                    $PHmin = $data[0]->PHmin;
                    $Wassermax = $data[0]->Wassermax;
                    $Wassermin = $data[0]->Wassermin;
                    $Dichtemax = $data[0]->Dichtemax;
                    $Dichtemin = $data[0]->Dichtemin;
                    return request()->validate([
                        'LieferNr' => ['required', 'min:5', 'max:5'],
                        'Produkt' => 'required',
                        'PH' => ['required', 'numeric', "min:$PHmin", "max:$PHmax"],
                        'Wasser' => "required|numeric|min:$Wassermin|max:$Wassermax",
                        'Dichte' => "required|numeric|min:$Dichtemin|max:$Dichtemax",
                        'Bearbeiter' => 'required',
                    ]);
            }
        } else {
            echo('Produktname ist falsch');
            return redirect()->back();
        }
    }

those are my uses

use App\Models\Validation;
use Illuminate\Http\Request;
use App\Models\Product;
use PhpParser\Node\Stmt\If_;

I dont know why I get this error and would be glad if I could get any help.

Upvotes: 0

Views: 2794

Answers (1)

steven7mwesigwa
steven7mwesigwa

Reputation: 6730

In the controller's store method body,

Instead of:

// ...
Product::create($product->check())->save(); ❌
// ...

Use this:

// ...
Product::create($this->check())->save(); ✅
// ...

Upvotes: 1

Related Questions