简体   繁体   English

在数组laravel 5.4上调用成员函数first()

[英]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. 我对laravel 5.4还是很陌生,对刀片模板不太了解。

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 问题是我正在将数组传递给视图,并尝试通过刀片模板提供的first()函数获取数组元素的第一个索引,但它给我错误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. 在您的方法中,您没有使用Laravel验证。 You just passing an array and basic php arrays does not have methods such as any or first . 您只是传递一个数组,而基本的php数组没有诸如anyfirst they belong to Laravel collections. 他们属于Laravel系列。

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 请深入阅读有关验证和认证的文档https://laravel.com/docs/5.4/validation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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