Home > Back-end >  PHP laravel Flash message is not showing
PHP laravel Flash message is not showing

Time:02-24

I am redirected to the desired page but success message is supposed to be shown there but there is no message has been shown. I have tried multiple methods online but there is something I missing.

Controller File:

public function store(Request $request) {
    $inputs = $request->all();

    // logger("I am here");
    $request->validate([
      'first_name' => 'required',
      'last_name' => 'required',
      'email' => 'required|email|unique:users,email',
      'phone_number' => 'required',
      'dob' => 'required|date'
    ]);

    // dd($inputs);

    $user = User::create([
      'first_name' => $inputs['first_name'],
      'last_name' => $inputs['last_name'],
      'email' => $inputs['email'],
      'phone_number' => $inputs['phone_number'],
      'dob' => $inputs['dob'],
      'role_id'=> 2
    ]);

    $user->session()->flash('success', 'your account has been created.'); 

    return redirect('/');
    // dd('success validation');
}

Blade File:

@if (session()->has('success'))
  <div >
    <p>{{ session('success') }}</p>
  </div>
@endif

CodePudding user response:

$user is a model, and can not use ->session().

You can redirect with flashed session data :

return redirect('/')->with('success', 'your account has been created.');

Blade :

@if (session('success'))
    <div >
        {{ session('success') }}
    </div>
@endif

CodePudding user response:

Try this

Session::flash('success', 'your account has been created!'); 

or there is another method called with() you can use it after redirect()

return redirect()
            ->with('success', 'your account has been created');
  • Related