Gammer
Gammer

Reputation: 5608

Laravel : Session data to view

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 ?

Upvotes: 0

Views: 1530

Answers (3)

user5594652
user5594652

Reputation:

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

It is that simple .

Upvotes: 1

Parvez Rahaman
Parvez Rahaman

Reputation: 25

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>

Upvotes: 0

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

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.

Upvotes: 0

Related Questions