Home > Mobile >  Call to a member function validate() on array
Call to a member function validate() on array

Time:05-08

I need your help for a project Im doing at the moment. I am using Laravel for programming and Im getting this error: 'Call to a member function validate() on array'

This is my store method

public function store()
    {
        $data = $this->check();

        switch ($data) {
            case (0):
                return redirect()->back()->with('error', 'Dieses Produkt ist nicht vorhanden');
            case (1):
                return redirect()->back()->with('error', 'Das Produkt mit dieser Liefer-Nummer ist bereits vorhanden');
            default:
                Product::create($data)->save();

                return redirect('/');
        }
    }

this is the check method

public function check()
    {
        $i = 0;
        foreach(Validation::all() as $valid)
        {
            $validation[$i] = [
                'id' => $valid->id,
                'Produkt' => $valid->Produkt,
                'PHmax' => $valid->PHmax,
                'PHmin' => $valid->PHmin,
                'Wassermax' => $valid->Wassermax,
                'Wassermin' => $valid->Wassermin,
                'Dichtemax' => $valid->Dichtemax,
                'Dichtemin' => $valid->Dichtemin,
            ];
            $i = $i   1;
        }
        $data = [
            'LieferNr' => request()->LieferNr,
            'Produkt' => request()->Produkt,
            'PH' => request()->PH,
            'Wasser' => request()->Wasser,
            'Dichte' => request()->Dichte,
            'Bearbeiter' => request()->Bearbeiter,
        ];
        $bigdata = Product::all();
        foreach(Validation::all() as $valid){
            foreach($bigdata as $bigdata){
                if($data['LieferNr'] == $bigdata->LieferNr){
                    return 1;
                }
            }

            if(in_array($data['Produkt'], $validation[0]) || in_array($data['Produkt'], $validation[1] ))
            {
                $PHmax = $valid->PHmax;
                $PHmin = $valid->PHmin;
                $Wassermax = $valid->Wassermax;
                $Wassermin = $valid->Wassermin;
                $Dichtemax = $valid->Dichtemax;
                $Dichtemin = $valid->Dichtemin;

                return $data->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
            {
                return 0;
            }
        }
    }

The error occurs when i do $data->validate(...) I am quite new to Laravel and i would be happy if you can help me :)

CodePudding user response:

I see your problem It is because you are calling validate on array $data

return $data->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'],
                ]);

Instead, do following

use Illuminate\Support\Facades\Validator;
$validator = Validator::make($data, [
                    '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'],
                ]);

if($validator->fails()){
 return 0;
}
  • Related