return user to a view with success message after registration laravel 9.x

Issue

I am trying to return a view with a success message with a link to the login page after registering a user, however the session message isn’t passed along with the view.

Here is my registred() method

protected function registered(Request $request, $user)
{
    return view('frontend.pages.register-success')->with('message', 'Your registration was successful. Click the button below to login to your account');
}

Here is the view page

@extends('frontend.layouts.main')
@section('title', 'Registration successful')
@section('content')
<!-- Main Content -->
<div id="main-content" class="site-main clearfix">

    @if (Session::has('message'))
    <section class="popup">
        <div class="popup-inner">
            <div class="popup-content">
                <div class="img">
                    <img src="assets/img/popup/popup.jpg" alt="Image">
                </div>
                <div class="content text-center">

                    <p class="">{{ Session::get('message') }}</p>

                    <h6 class="heading">REGISTRATION SUCCESSFUL!</h6>

                    <a href="{{ route('login') }}" class="button color-2 login-submit">LOGIN TO YOUR ACCOUNT</a>
                </div>
            </div>
        </div>
    </section>
    @endif

</div><!-- /#main-content -->
@endsection

Solution

We can use session() global helper instead of Session:

      // flash message create
      session()->flash('message', 'This is a message!'); 
      session()->flash('alert-class', 'alert-danger'); 

get flash message by key from your blade file

@if(session()->has('message'))
<section class="popup">
        <div class="popup-inner">
            <div class="popup-content">
                <div class="img">
                    <img src="assets/img/popup/popup.jpg" alt="Image">
                </div>
                <div class="content text-center">

                    <p class="">{{ session('message') }}</p>

                    <h6 class="heading">REGISTRATION SUCCESSFUL!</h6>

                    <a href="{{ route('login') }}" class="button color-2 login-submit">LOGIN TO YOUR ACCOUNT</a>
                </div>
            </div>
        </div>
    </section>
@endif

Answered By – Waqas Altaf

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published