Luiiiz
Luiiiz

Reputation: 53

Call to a member function fails() on array

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();
    }
}

Upvotes: 5

Views: 25085

Answers (2)

Kenny Horna
Kenny Horna

Reputation: 14241

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.

Upvotes: 9

Niellles
Niellles

Reputation: 878

What this error is telling you is that by doing $query->fails you're calling a method fails() on something (i.e. $query) that's not an object, but an array. As stated in the documentation $this->validate() returns an array of errors.

To me it looks like you've mixed a bit of the example code on validation hooks into your code.


If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated, [...] -Laravel Docs

The following code should do the trick. You then only have to display the errors in your view. You can read all about that, you guessed it, in... the docs.

public function postRegister(Request $request)
{
    $query = $request->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',
    ]);
}

Upvotes: 0

Related Questions