我对 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
我正在寻找完全适合我需求的解决方案。如果我做错了什么,请纠正我。谢谢...
在您的方法中,您没有使用 Laravel 验证。您只是传递一个数组,而基本的 php 数组没有诸如any
or 之类的方法first
。它们属于 Laravel 集合。
它只是一个数组,你可以像我下面解释的那样访问数组元素
所以如果你想保留你的代码,你可以这样做
@if(isset($error))
{{$error['email'] }}
@endif
但正确的做法是验证部分;
$this->validate($request, [
'email' => 'required| email',
]);
请深入阅读有关验证和身份验证的文档https://laravel.com/docs/5.4/validation
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句