I have a admin middleware which check out if user is logged in. If he is not I want to redirect him to login page. But in login form (it is modal on articles page) I would like to show error with message. So I tried to call:
return redirect()->route('articles')->withErros(['login' => 'You have to log in to access admin module.']);
This does not show the error cause withErrors is not a metohod in RedirectResponse object. Can somebody tell me what is the difference between back() and redirect()->route()? Cause as I see
return back()->withErrors(['login' => 'You have to log in to access admin module.']);
works well and I am able to show errors in login form.
Here is the whole middleware
public function handle(Request $request, Closure $next)
{
$user = $request->user();
if( !$user )
{
session()->flash('showModal', 'loginModal');
// This does not work
return redirect()->route('articles')->withErros(['login' => 'You have to log in to access admin module.']);
// This works but is is restricted to back url...
return back()->withErrors(['login' => 'You have to log in to access admin module.']);
}
elseif( !$user->hasRole([Role::ROLE_ADMIN, Role::ROLE_REDACTOR]) )
{
flash('You do not have permission do access admin module.')->error()->important();
return redirect()->route('articles');
}
return $next($request);
}