我试图重定向到上一页的消息时,有一个致命的错误。
App::fatal(function($exception)
{
return Redirect::back()->with('msg', 'The Message');
}
在视图中尝试访问msg
Sessions::get('msg')
但是什么都没有渲染,是我做错了吗?
我试图重定向到上一页的消息时,有一个致命的错误。
App::fatal(function($exception)
{
return Redirect::back()->with('msg', 'The Message');
}
在视图中尝试访问msg
Sessions::get('msg')
但是什么都没有渲染,是我做错了吗?
当前回答
与 Laravel 5.8 相关:
控制器
return back()->with('error', 'Incorrect username or password.');
叶片
@if (Session::has('error'))
<div class="alert alert-warning" role="alert">
{{Session::get('error')}}
</div>
@endif
其他回答
Try
return Redirect::back()->withErrors(['msg' => 'The Message']);
在视图中调用这个
@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
Laravel 5.6。*
控制器
if(true) {
$msg = [
'message' => 'Some Message!',
];
return redirect()->route('home')->with($msg);
} else {
$msg = [
'error' => 'Some error!',
];
return redirect()->route('welcome')->with($msg);
}
叶片模板
@if (Session::has('message'))
<div class="alert alert-success" role="alert">
{{Session::get('message')}}
</div>
@elseif (Session::has('error'))
<div class="alert alert-warning" role="alert">
{{Session::get('error')}}
</div>
@endif
Enyoj
# Laravel-9 刀片内部,该重定向返回操作启动的地方
return redirect()->back()->with('message', "The Message");
刀片内部的这种形式,将在上述动作后返回
@if(session()->has('message'))
<p class="alert alert-success"> {{ session()->get('message') }}</p>
@endif
在Laravel 5.4中,以下内容对我有用:
return back()->withErrors(['field_name' => ['Your custom message here.']]);
与 Laravel 5.8 相关:
控制器
return back()->with('error', 'Incorrect username or password.');
叶片
@if (Session::has('error'))
<div class="alert alert-warning" role="alert">
{{Session::get('error')}}
</div>
@endif