简体   繁体   中英

Call to a member function first() on array laravel 5.4

I am very to new to laravel 5.4 and don't know about much about the blade template.

The issue is I am passing the array to the views and trying to get the first index of array element through the provided first() function of blade template but it's giving me error Call to a member function first() on array

Here is my controller code

 public function authenticate(Request $request )
 {               
    if (Auth::attempt(['email' => $request->input('email'), 'password' => 
        $request->input('password'), 'Status' => 0])) 
        {
            // Authentication passed...
            return redirect()->intended('Users');
        }
    else
    {
        $json=array('email'=>'You cant leave Email field empty');         
        return  View::make('Auth/login')->with('error',($json));           
    }
 }

Here is my View Code

  @if($errors->any())         
       {{ $errors->first('email') }}
  @endif

I am looking for the solution which can exactly suit my needs. If am doing something wrong please correct me. Thanks...

You may use

@foreach ($errors as $error)
   {{ $error }}
@endforeach

So you can see the list of the error returned

In your approach you are not using Laravel validations. You just passing an array and basic php arrays does not have methods such as any or first . they belong to Laravel collections.

It is just an array and you can reach array elements as I explained below

so if you wanna keep your code you can do this

@if(isset($error))
   {{$error['email'] }} 
@endif

But correct way to do is for validation part;

$this->validate($request, [
        'email' => 'required| email',
    ]);

please read documentation deeply about validations and authentication https://laravel.com/docs/5.4/validation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM