在if语句中返回带有$ errors的重定向

I think its a simple question to answer for you guys. I just want to redirect to the given page when serialnumber has an error but it tells me that $errors is undefined so i thought i have to add something like this:

use Illuminate\Http\Request;

but i coulndt find something like that what did i miss can anyone help me? :)

if ($errors->has('serialnumber')){

        return redirect()->route('borrow.index')
                         ->with('warning','Test');

}

EDIT:

I would like that with no matter what error you get rejected with a message on the page I have specified. With a single message that I define myself. The link in the comments wont help me out......

According to Laravel's documentation, The $errors variable is bound to the view by the Illuminate\View\Middleware\ShareErrorsFromSession middleware, which is provided by the web middleware group.

As you shouldn't do redirects from your views, just check your condition inside your controller instead of your view.

If you want to redirect with a specific error you can use you can use this from the Laravel documentation:

 $validator = Validator::make($request->all(), [
                'title' => 'required|unique:posts|max:255',
                'body' => 'required',
            ]);

then get the errors :

$errors = $validator->errors();

or redirect when validator fails :

if ($validator->fails()) {
    // your redirect here...
}