Laravel : Session data to view

蹲街弑〆低调 提交于 2019-12-25 07:35:32

问题


I have the following code, In which i am checking for error.

<div class="alert alert-danger {{ (\Session::has('message') && \Session::get('form', 'login') == 'login') ? '' : 'display-hide' }}">
    <button class="close" data-close="alert"></button>
    <span>
        {!! \Session::has('message') ? \Session::get('message') : 'Please correct your fields.' !!}
    </span>
</div>

On the controller side i have :

return redirect()
        ->back()
        ->with('message', 'Incorrect email or password.')
        ->with('form', 'login')
        ->withInput(\Input::except('password'));

The thing is that the message is not showing there.

Just the page refreshes and no message comes up.

Any idea ? Am i missing something ?


回答1:


Simply on your view use \Session::pull('message') instead of \Session::get('message').

It is that simple .




回答2:


In your Controller

return redirect()
        ->back()
        ->with('message', 'Incorrect email or password.')
        ->with('form', 'login')
        ->withInput(\Input::except('password'));

And in your View

@if(Session::has('message')  && Session::has('form'))
<div class="alert alert-dismissable alert-success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{!! Session::get('message') !!}
</div>
</span>
@endif

Note : You can have your own html in the condition, throw your message in your way and modify condition according to your need.




回答3:


As the laravel 5.2 documentation says

<div class="alert alert-danger {{ (session('message') && session('form') === 'login') ? '' : 'display-hide' }}">
    <button class="close" data-close="alert"></button>
    <span>
        {!! session('message') ? session('message') : 'Please correct your fields.' !!}
    </span>
</div>

                        //OR

return redirect()
    ->back()
    ->with('message', 'Incorrect email or password.')
    ->with('form', 'login')
    ->with('classType', 'display-hide')
    ->withInput(\Input::except('password'));



<div class="alert alert-danger {{ session('classType') or '' }}">
    <button class="close" data-close="alert"></button>
    <span>
        {{ session('message') or 'Please correct your fields.' }}
    </span>
</div>


来源:https://stackoverflow.com/questions/37438174/laravel-session-data-to-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!