Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

afoysal's avatar

Use Flash Message in Blade template

I have controller and function like below

class UserController extends Controller
    {

        public function index(){
            $title = trans('app.users');
            $users = User::orderBy('first_name', 'asc')->paginate(20);
             $users_count = User::count();
            return view('admin.users', compact('title', 'users', 'users_count'));
     }
}

I have a blade template named flash_msg.blade.php which has below content.

@if(session('success'))
        <div class="alert alert-success">
            {!! session('success') !!}
        </div>
@endif

@if(session('error'))
        <div class="alert alert-danger">
            {!! session('error') !!}
    </div>

@endif

I included below code in my blade template.

@include('admin.flash_msg')

How can I use this ? Which code should I write in controller to get the result of Flash Message ?

Thanks

0 likes
3 replies
Tray2's avatar

I your controller methods you can do this

return redirect('dashboard')->with('success', 'Profile updated!');

You should use {{ }} instead of {!! !!}

2 likes
afoysal's avatar

How can I use your solution with return view() ?

Snapey's avatar

You should not have any use cases where you need to return it with view.

Anything that changes the server content should return a redirect.

1 like

Please or to participate in this conversation.