A trait for testing laracasts/flash

The Laracasts/flash package is an amazing solution to send Bootstrap 3 styled flash messages to the user’s screen.

After you have set it up, you can do something like this in your controller:

public function store()


    flash('Welcome Aboard!');

    return home();


And you get a flash message looking like this:

Example flash message
Notice the green bar here

But how do you test the flash message was actually sent using phpunit?

Luckily, someone already had figured out a solution. Paul-Henri L (@paulhenri-l) shared an easy-to-overlook snippet at the repository.

I adapted it a bit to suit my needs and refactored it into a trait. I have been using it a lot over the last weeks.

Here is the CanAssertFlash trait, and an example of how to use it.



Create your first Laravel package

Laravel package development from scratch

Learning Laravel package development doesn't have to be hard. If you know how to create a basic Laravel app, you can write packages as well.

I've opened this course up for free to the first 100 subscribers only.

Join now and get the first lesson in your inbox right away.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit

Leave a Reply

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