return Redirect::back()->withErrors(['msg', 'The Message']);
and inside your view call this
@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
欢迎各位兄弟 发布技术文章
这里的技术是共享的
withErrors 的用法 见
vendor/laravel/framework/src/Illuminate/Http/RedirectResponse.php
好像 with 只有 withErrors withCookie withInput 和 with 这几种用法 应该不能使用 withMessages 等
至少使用了之后 $messages->any()方法不能使用 ,至少在 laravel 5.2 中是这样
return Redirect::back()->withErrors(['msg', 'The Message']);
and inside your view call this
@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
来自 https://stackoverflow.com/questions/19838978/laravel-redirect-back-with-message
You should remove web
middleware from routes.php
. Adding web
middleware manually causes session and request related problems in Laravel 5.2.27 and higher.
If it didn't help (still, keep routes.php
without web middleware), you can try little bit different approach:
return redirect()->back()->with('message', 'IT WORKS!');
Displaying message if it exists:
@if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif
来自 https://stackoverflow.com/questions/37376168/laravel-5-2-redirect-back-with-success-message
I just did something like this. You need only use blade's templating!
//pass back a variable when redirecting
return Redirect::back()->with('error_code', 5);
And then in your blade template:
@if(!empty(Session::get('error_code')) && Session::get('error_code') == 5)
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
@endif
This will create a script that opens the dialog whenever error_code is present and equals 5!
Set the flash message and then redirect to desired route
Controller:
session()->flash('msg', 'Successfully done the operation.');
return Redirect::to('admin/user/create');
Now get the message the in view blade file
Blade
{!! Session::has('msg') ? Session::get("msg") : '' !!}
来自 https://stackoverflow.com/questions/35691134/laravel-redirect-back-with-not-sending-any-messages
Laravel 5
正确答案
Controller
return redirect()->back()->with('success', ['your message,here']);
Blade:
@if (\Session::has('success'))
<div class="alert alert-success">
<ul>
<li>{!! \Session::get('success') !!}</li>
</ul>
</div>
@endif
If it gives Error : Array to String Conversion, then just small fix in controller
return redirect()->back()->with('success', 'your message here');
来自 https://stackoverflow.com/questions/19838978/laravel-redirect-back-with-message