Starfish
Starfish

Reputation: 3574

Laravel 5.1 flash message handling

In Laravel 5.0 and 4.* there was this option to check if the Session had flash_messages:

Controller:

public function store(Request $request) {
    Session::flash('flash_message', 'Success');
    return redirect('somepage');
}

And then you could call this in your layout:

@if (Session::has('flash_message'))
    <p>{{ Session::get('flash_message') }}</p>
@endif

This worked perfectly fine, however in Laravel 5.1 it changed. Now it's stored in the request variable. So, I changed it in the controller to:

public function store(Request $request) {
    $request->session()->flash('flash_message', 'Success');
    return redirect('somepage');
}

And I call this in the layout:

@if($request->session()->has('flash_message'))
    <p>{{ $request->session()->get('flash_message') }}</p>
@endif

I'm doing exactly what is stated in the Laravel 5.1 docs:

Determining If An Item Exists In The Session

The has method may be used to check if an item exists in the session. This method will return true if the item exists:

if ($request->session()->has('users')) { // }

Now this works perfectly fine for store method now, because I only added a flash_message there, but if I want to access another method like index, create, edit, etc. it shows this error:

ErrorException

Undefined variable: request

I understand that the request variable is not declared in the other methods and I can workaround this if-statement using isset($request).

Is there a better solution to check if my flash_message is set in the $request variable like in the earlier versions of Laravel?

Upvotes: 0

Views: 1421

Answers (2)

Rameez Rami
Rameez Rami

Reputation: 5728

You can still use the old method :) . latest laravel just added new ways to achieve that goal.here goes the sample code to display error messages which are passed as flash message into a view page .

Sample code

@if ($message = Session::get('success'))
 <div class="alert alert-success alert-block">
  <button type="button" class="close" data-dismiss="alert">&times;</button>
  <h4>Success</h4>
  @if(is_array($message))
   @foreach ($message as $m)
    {{ $m }}
   @endforeach
  @else
   {{ $message }}
  @endif
 </div>
@endif

Upvotes: 0

James O&#39;Neill
James O&#39;Neill

Reputation: 416

It sounds like you're using controllers generated by Laravel.

If you look at the method definitions for store and update you'll find the parameter Request $request. By including this Laravel automatically creates a Request instance for you.

In order to access this in other methods you have three options:

Add the same parameter to any of the other method definitions and Laravel will generate it for you in the same way.

Create a constructor function and have it assign a Request instance to an attribute.

public function __construct(Request $request)
{
     $this->request = $request;
}

Declare use Session; before the class definition to make the Session class accessible in your namespace.

Upvotes: 1

Related Questions