Home > Enterprise >  Flash message is not displayed when redirecting to route
Flash message is not displayed when redirecting to route

Time:02-01

I have the following code to associate a contact to a lead:

public function selectSalesConsultant($uuid)
{
    $lead = Lead::where('uuid', $uuid)->firstOrFail();

    return view('dealer-contacts.select_consultant')
        ->with(compact('lead'));
}

public function updateSalesConsultant(Request $request, $uuid)
{
    $lead = Lead::where('uuid', $uuid)->firstOrFail();

    $lead->update([
        'contact_id' => $request->get('contact_id')
    ]);

    Flash::success('Sales Consultant information updated for the lead.');

    return to_route('select-sales-consultant', ['uuid' => $uuid]);
}

In my second function, after the lead is updated, I'd like to display a success message:

Flash::success('Sales Consultant information updated for the lead.');

return to_route('select-sales-consultant', ['uuid' => $uuid]);

Here is what I have in my view:

@include('flash::message')

Here is how the route is defined inside routes/api.php:

Route::get('/select-sales-consultant/{uuid}', [App\Http\Controllers\LeadController::class, 'selectSalesConsultant'])
    ->name('select-sales-consultant');

Nothing gets displayed. If, instead of redirecting, I do a return view(), the message does get displayed.

What's the proper way of doing this? I'm using the Laracasts Easy flash notifications package.

CodePudding user response:

Try changing the code to this:

Flash::success('Sales Consultant information updated for the lead.');

return redirect()->route('select-sales-consultant', ['uuid' => $uuid])
->with(['message' => Flash::message()]);

The with method allows you to pass data to the session for a single request, so the message will be available in the session after the redirect. Then, you can display the message in your view.

Hopes this solve your problematic.

CodePudding user response:

Looks like it was because I was using an API route.

I added the following to my Kernel.php, and it resolved the issue.

'api' => [
   \Illuminate\Session\Middleware\StartSession::class,
   //etc
],
  • Related