Bulent
Bulent

Reputation: 3411

How To Display Laravel Breeze Status Message

I'm using Laravel Breeze for authentication, and I'm facing a problem:

When user request a password reset link, I like to show him/her a success message, if we send email successfully. PasswordResetLinkController returns this:

return $status == Password::RESET_LINK_SENT
    ? back()->with('status', __($status))
    : back()->withInput($request->only('email'))
            ->withErrors(['email' => __($status)]);

When it goes back, it goes, for example, to home route. HomeController returns home.blade.php. When I try to display $status, which should be passed by PasswordResetLinkController, I got undefiened variable error. How can I get that message?

EDIT

PasswordResetLinkController.php

// This is the original store function came with Breeze.
// I did touch neither code nor the comments.
public function store(Request $request)
{
    $request->validate([
       'email' => 'required|email',
    ]);

    // We will send the password reset link to this user. Once we have attempted
    // to send the link, we will examine the response then see the message we
    // need to show to the user. Finally, we'll send out a proper response.
    $status = Password::sendResetLink(
        $request->only('email')
    );
    return $status == Password::RESET_LINK_SENT
       ? back()->with('status', __($status))
       : back()->withInput($request->only('email'))
               ->withErrors(['email' => __($status)]);
}

HomeController.php

public function index()
{
   $baseData = $this->baseData();

   $asset = $this->pickAssetRandom();

   $publishings = $this->paginate($this->getPublishings, 12);

   return view('pages.home', compact('publishings', 'baseData', 'asset'));
}

Upvotes: 1

Views: 2951

Answers (2)

Theodory
Theodory

Reputation: 397

Treat that status as a session and it will work

@if (session('status'))
    <span class="alert alert-success">{{ session('status') ?? ''}} </span>
@endif

Upvotes: 0

gclark18
gclark18

Reputation: 725

The $status is being set in the PasswordResetLinkController.

Specifically:

back()->with('status', __($status))

So, as you can see, it is returning the previous page and passing in status.

However, if $status == Password::RESET_LINK_SENT is false, then $status is not set, but the $errors['email'] is. You can see this on the ternary condition in your code.

Try:

dd($status == Password::REST_LINK_SENT);

before the return statement on the controller, if you get false then there will be no $status, and you will get the undefiened variable error.

You can account for this in your view:

@if ($status) 
    {{ $status }} // A link was sent
@endif


// no link sent and here are the errors.
@if ($errors->any())
    @foreach ($errors->all() as $error)
    {{ $error }}
    @endforeach
@endif

Laravel docs on this: https://laravel.com/docs/8.x/passwords#resetting-the-password

Upvotes: 1

Related Questions