Php 如何使用Laravel 5提供内联错误反馈

Php 如何使用Laravel 5提供内联错误反馈,php,validation,laravel-5,Php,Validation,Laravel 5,我正在使用Laravel 5构建一个表单,我希望错误消息出现在身份验证页面的字段旁边。 这是我的控制器 登录 你会写 <input type="text" placeholder="Username" class="form-control" name="username" /> @if ($errors->has('username')) <span class="error-text text-danger">{{ $errors->first('u

我正在使用Laravel 5构建一个表单,我希望错误消息出现在身份验证页面的字段旁边。 这是我的控制器


登录

你会写

<input type="text" placeholder="Username" class="form-control" name="username" />
@if ($errors->has('username')) <span class="error-text text-danger">{{ $errors->first('username') }}</span> @endif

<input type="password" placeholder="Password" class="form-control" name="password" />
@if ($errors->has('password')) <span class="error-text text-danger">{{ $errors->first('password') }}</span> @endif


@if($errors->has('username')){{{$errors->first('username')}}@endif
@if($errors->has('password')){{{$errors->first('password')}}@endif
你会写

<input type="text" placeholder="Username" class="form-control" name="username" />
@if ($errors->has('username')) <span class="error-text text-danger">{{ $errors->first('username') }}</span> @endif

<input type="password" placeholder="Password" class="form-control" name="password" />
@if ($errors->has('password')) <span class="error-text text-danger">{{ $errors->first('password') }}</span> @endif


@if($errors->has('username')){{{$errors->first('username')}}@endif
@if($errors->has('password')){{{$errors->first('password')}}@endif

您需要在控制器中使用验证

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use DB;

class LoginController extends Controller {

    public function login(Request $request){

        $this->validate($request, [
            'username' => 'required',
            'password' => 'required'
        ]);

        $username = $request->input('username');
        $password = $request->input('password');
        $checklogin = DB::table('users')->where(['Username' => $username, 'Password' => $password])->get();
        if(count($checklogin)){
            echo "login successful";
        }else{
            return view('login');
        }
    }
}
然后,在刀片服务器中,您可以使用如下所示的修复错误:

<input type="text" placeholder="Username" class="form-control" name="username" />
@if ($errors->has('username'))
  <span class="error-text text-danger">{{ $errors->first('username') }}</span>
@endif
<input type="password" placeholder="Password" class="form-control" name="password" />
@if ($errors->has('password'))
  <span class="error-text text-danger">{{ $errors->first('password') }}</span>
@endif

@如果($errors->has('username'))
{{$errors->first('username')}
@恩迪夫
@如果($errors->has('password'))
{{$errors->first('password')}
@恩迪夫

您需要在控制器中使用验证

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use DB;

class LoginController extends Controller {

    public function login(Request $request){

        $this->validate($request, [
            'username' => 'required',
            'password' => 'required'
        ]);

        $username = $request->input('username');
        $password = $request->input('password');
        $checklogin = DB::table('users')->where(['Username' => $username, 'Password' => $password])->get();
        if(count($checklogin)){
            echo "login successful";
        }else{
            return view('login');
        }
    }
}
然后,在刀片服务器中,您可以使用如下所示的修复错误:

<input type="text" placeholder="Username" class="form-control" name="username" />
@if ($errors->has('username'))
  <span class="error-text text-danger">{{ $errors->first('username') }}</span>
@endif
<input type="password" placeholder="Password" class="form-control" name="password" />
@if ($errors->has('password'))
  <span class="error-text text-danger">{{ $errors->first('password') }}</span>
@endif

@如果($errors->has('username'))
{{$errors->first('username')}
@恩迪夫
@如果($errors->has('password'))
{{$errors->first('password')}
@恩迪夫

编写您试图解决问题的代码请使用您尝试的代码编辑您的答案查找代码图像描述的链接编写您试图解决问题的代码请使用您尝试的代码编辑您的答案查找代码图像描述的链接