簡體   English   中英

在數組laravel 5.4上調用成員函數first()

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

我對laravel 5.4還是很陌生,對刀片模板不太了解。

問題是我正在將數組傳遞給視圖,並嘗試通過刀片模板提供的first()函數獲取數組元素的第一個索引,但它給我錯誤Call to a member function first() on array

這是我的控制器代碼

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

這是我的查看代碼

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

我正在尋找完全適合我需求的解決方案。 如果做錯了,請糾正我。 謝謝...

您可以使用

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

這樣就可以看到返回的錯誤列表

在您的方法中,您沒有使用Laravel驗證。 您只是傳遞一個數組,而基本的php數組沒有諸如anyfirst 他們屬於Laravel系列。

這只是一個數組,您可以達到數組元素,如下所述

因此,如果您想保留自己的代碼,可以這樣做

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

但是正確的方法是驗證部分。

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

請深入閱讀有關驗證和認證的文檔https://laravel.com/docs/5.4/validation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM