我试图重定向到上一页的消息时,有一个致命的错误。
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及以后
控制器
return redirect()->back()->with('success', 'your message,here');
叶:
@if (\Session::has('success'))
<div class="alert alert-success">
<ul>
<li>{!! \Session::get('success') !!}</li>
</ul>
</div>
@endif
其他回答
对于拉拉维尔3
只是提醒一下@giannis christofakis的回答;对于任何使用Laravel 3替代
return Redirect::back()->withErrors(['msg', 'The Message']);
:
return Redirect::back()->with_errors(['msg', 'The Message']);
我也遇到过同样的问题,这个方法奏效了。
控制器
return Redirect::back()->withInput()->withErrors(array('user_name' => $message));
View
<div>{{{ $errors->first('user_name') }}}</div>
只需设置flash消息和重定向回从你的控制器功能。
session()->flash('msg', 'Successfully done the operation.');
return redirect()->back();
然后你可以在视图刀片文件中得到消息。
{!! Session::has('msg') ? Session::get("msg") : '' !!}
在laravel 5.8中,你可以做以下事情:
return redirect()->back()->withErrors(['name' => 'The name is required']);
在刀刃上:
@error('name')
<p>{{ $message }}</p>
@enderror
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