Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function fails() on array

Tags:

php

laravel

I have a problem with the laravel validation.

Call to a member function fails() on array

Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Call to a member function fails() on array"

Stacktrace:

`#0 Symfony\Component\Debug\Exception\FatalThrowableError in C:\laragon\www\frontine\app\Http\Controllers\authController.php:37

public function postRegister(Request $request)
{
    $query = $this->validate($request, [
        'user' => 'string|required|unique:users|min:4|max:24',
        'email' => 'email|string|required|unique:users',
        'pass' => 'string|required|min:8',
        'cpass' => 'string|required|min:8|same:pass',
        'avatar' => 'image|mimes:jpeg,jpg,png|max:2048',
    ]);

    if ($query->fails())
    {
        return redirect('/registrar')
            ->withErrors($query)
            ->withInput();
    }
}
like image 977
Luiiiz Avatar asked Apr 29 '19 20:04

Luiiiz


1 Answers

The error is because what the ->validate() method returns an array with the validated data when applied on the Request class. You, on the other hand, are using the ->fails() method, that is used when creating validators manually.

From the documentation:

Manually Creating Validators

If you do not want to use the validate method on the request, you may create a validator instance manually using the Validator facade. The make method on the facade generates a new validator instance:

use Validator; // <------
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [ // <---
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);

        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }

        // Store the blog post...
    }
}

The ->fails() is called in the response of the Validator::make([...]) method that return a Validator instance. This class has the fails() method to be used when you try to handled the error response manually.

On the other hand, if you use the validate() method on the $request object the result will be an array containing the validated data in case the validation passes, or it will handle the error and add the error details to your response to be displayed in your view for example:

    public function store(Request $request)
    {
       $validatedData = $request->validate([
            'attribute' => 'your|rules',
        ]);

       // I passed!

     }

Laravel will handled the validation error automatically:

As you can see, we pass the desired validation rules into the validate method. Again, if the validation fails, the proper response will automatically be generated. If the validation passes, our controller will continue executing normally.

like image 184
Kenny Horna Avatar answered Sep 30 '22 11:09

Kenny Horna