Laravel session flash message

In the controller file:

public function store(PostRequest $request) {
    if (Post::create($request->all())) {
        $request->session()->flash('message.level', 'success');
        $request->session()->flash('message.content', 'Post was successfully added!');
    } else {
        $request->session()->flash('message.level', 'danger');
        $request->session()->flash('message.content', 'Error!');
    }
    return redirect('/');
}

In view file:

@if(session()->has('message.level'))
    <div class="alert alert-{{ session('message.level') }}"> 
    {!! session('message.content') !!}
    </div>
@endif

So Here its very simple and easy code for session flash message in the laravel.

Below code can use in Codeigniter:

//Set Message
$this->session->set_flashdata(['status' => 0, 'message' => 'Data does not exist']);

//Get Message
$this->session->flashdata('message')

1 thought on “Laravel session flash message”

  1. I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

Leave a Comment

Your email address will not be published. Required fields are marked *