我试图重定向到上一页的消息时,有一个致命的错误。
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')
但是什么都没有渲染,是我做错了吗?
当前回答
拉拉维尔 8
Route::post('/user/profile', function () {
// Update the user's profile...
return redirect('/dashboard')->with('status', 'Profile updated!');
});
叶片的语法
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
在这里输入链接描述
其他回答
对于拉拉维尔 5.6.*
在尝试Laravel 5.6提供的一些答案时。*,很明显已经有了一些改进,我将在这里发布,以方便那些无法找到解决方案的其余答案。
STEP 1:转到你的控制器文件,在类之前添加这个:
use Illuminate\Support\Facades\Redirect;
步骤2: 将此添加到想要返回重定向的地方。
return Redirect()->back()->with(['message' => 'The Message']);
步骤3: 转到刀片文件并按如下方式编辑
@if (Session::has('message'))
<div class="alert alert-error>{{Session::get('message')}}</div>
@endif
然后测试一下,然后感谢我。
这应该适用于laravel 5.6。*和可能5.7.*
这是100%的解决方案
*上面提到的解决方案不适合我,但这一个适用于我在laravel 5.8:
$status = 'Successfully Done';
return back()->with(['status' => $status]);
并接受它为:
@if(session()->has('status'))
<p class="alert alert-success">{{session('status')}}</p>
@endif
与 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
只需设置flash消息和重定向回从你的控制器功能。
session()->flash('msg', 'Successfully done the operation.');
return redirect()->back();
然后你可以在视图刀片文件中得到消息。
{!! Session::has('msg') ? Session::get("msg") : '' !!}
拉拉维尔 8
Route::post('/user/profile', function () {
// Update the user's profile...
return redirect('/dashboard')->with('status', 'Profile updated!');
});
叶片的语法
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
在这里输入链接描述