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

TUSHAR2004's avatar

Session messages sometimes flash sometimes does not.

Hi,

I tried following but none of the method is working and in both cases, session message sometimes flash and sometimes doesn't.

@if (Session::has('sms_not_sent')) 
        <div class="form-group">
            <div class="alert alert-danger">
                <i class="fas fa-times text-danger"></i> <strong>{{Session('sms_not_sent')}}</strong>
            </div>
        </div>
@endif
@if (Session::has('sms_not_sent')) 
        <div class="form-group">
            <div class="alert alert-danger">
                <i class="fas fa-times text-danger"></i> <strong>{{Session::get('sms_not_sent')}}</strong>
            </div>
        </div>
@endif

Thanks

0 likes
6 replies
MA's avatar

Hello!

Show us the code where you are storing the data into the session!

TUSHAR2004's avatar

Hi,

Here's the code where I am storing the data into the session.

            try{
                TextLocal::sendSms([$user->phone], $validatedData['body'], "TXTLCL", null, false, null, null, false, false);
            } catch(\Exception $x) {
                $request->session()->flash('sms_not_sent','SMS could not be sent. Please contact the administrator.');
                session()->forget('sms_sent');
            }
MA's avatar

I don't see any problems at all. The session()->flash() stores a key into the session only for the next request. Because you are using it inside a try...catch block, you get the flash value only if you get an exception.

TUSHAR2004's avatar

Hello @ma. Thanks for your reply. Sometimes, it doesn't flash a message on getting an exception even.

And the try.....catch block is just for this case, it sometimes doesn't work in an if-else statement as well. For example, if the user updates his profile a success message is set otherwise an error message is set.

Code:

        if($user->first_name !== $validatedData['first_name'] || $user->last_name !== $validatedData['last_name'] || isset($image) || $user->email !== $validatedData['email'] || $user->phone != $validatedData['phone']){
            if(isset($image)) { 
                if(Storage::exists('public/users/' . $user->path)) {
                    Storage::delete('public/users/' . $user->path);
                }
                $image->storeAs('public/users',$filename);
                $validatedData['photo'] = $filename;
            }

            $user->update($validatedData);
            $request->session()->flash('user_profile_updated','Your profile has been successfully updated.');
        }else{
            $request->session()->flash('no_user_profile_updated','Could not update the profile as no changes have been made.');
        }

And the code which I am using to display these messages:

    @if (Session::has('user_profile_updated'))
        <div class="form-group">
            <div class="alert alert-success">
                <i class="fas fa-check text-success"></i> <strong>{{Session('user_profile_updated')}}</strong>
            </div>
        </div>
    @endif

    @if (Session::has('no_user_profile_updated'))
        <div class="form-group">
            <div class="alert alert-danger">
                <i class="fas fa-times text-danger"></i> <strong>{{Session('no_user_profile_updated')}}</strong>
            </div>
        </div>
    @endif
TUSHAR2004's avatar

No, I haven't tried that yet.

I am either redirecting to a specific page like this return redirect('/admin/profile'); or redirect()->back()

Have you try redirecting with flashed session data? How you redirect/return the response?

I'll give it a shot and let you know what happened.

Please or to participate in this conversation.