Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toastr Notifications Not Displaying in Laravel 11.9 Controller #51

Open
RehamTammam1 opened this issue Aug 14, 2024 · 3 comments
Open

Comments

@RehamTammam1
Copy link

RehamTammam1 commented Aug 14, 2024

Hello, I'm using Laravel 11.9 and following the instructions to integrate Toastr notifications with the Flasher package in my application. However, the notifications are not displaying as expected.
Problem:
After saving a new grade, I expect a Toastr notification to appear confirming that the data was saved successfully. However, no notifications are displayed.
Expected Behavior:
A Toastr notification should display a success message when the grade is successfully saved.

<?php 
namespace App\Http\Controllers\Grades;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreGradeRequest;
use App\Models\Grade as ModelsGrade;
use Flasher\Toastr\Prime\ToastrInterface;
use Illuminate\Http\Request;
use Flasher\Notyf\Prime\NotyfInterface;
use Flasher\Toastr\Prime\Toastr;

use function Flasher\Toastr\Prime\toastr;

class GradeController extends Controller 
{
  public function index()
  {
    $grades = ModelsGrade::all();
    return view('grades.gradelist', compact('grades'));
  }

  public function store(StoreGradeRequest $request)
  {
    $validated = $request->validated();

    $grade = new ModelsGrade();
    $grade->Name = ['en' => $request->Name_en, 'ar' => $request->Name];
    $grade->Notes = $request->Notes;
    $grade->save();

    if ($grade instanceof ModelsGrade) {
      flash()->success('Operation completed successfully.');
      toastr()->success('Data has been saved successfully!');
      return redirect()->route('grades.index');
    }
  }
  
  // Other methods omitted for brevity
}
?>

Actual Behavior:
No notifications are displayed, even though the session data contains the notification information.

Additional Information:

Laravel Version: 11.9
PHP Version: 8.2
Flasher Package: php-flasher/flasher-toastr
Could you please assist in identifying why the Toastr notifications aren't displaying?

Thank you!

@yoeunes
Copy link
Owner

yoeunes commented Aug 14, 2024

Hello Reham,

Thank you for providing all the details!

Could you please try registering the \Flasher\Laravel\Middleware\FlasherMiddleware::class middleware in your application? You can add it to the $middleware array in the app/Http/Kernel.php file like this:

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array<int, class-string|string>
     */
    protected $middleware = [
        // Other middleware...
        \Flasher\Laravel\Middleware\FlasherMiddleware::class,
    ];

    // Other properties and methods...
}

This should ensure that the middleware is properly registered and can handle the flash notifications.

If adding the middleware doesn't resolve the issue, please share a project on GitHub where you can reproduce the issue, and I'd be glad to help you fix it.

Looking forward to your feedback!

Best regards,
Younes

@RehamTammam1
Copy link
Author

Hello @yoeunes,

Thank you for your guidance—it worked perfectly for me! After registering the middleware in kernel.php, the notifications populated successfully. I believe it would greatly enhance user experience if this step or a note about it could be added as part of the installation/configuration steps. It would save users a lot of time.

However, I'm facing another issue where the notification is triggered with each page refresh, even in cases where it shouldn't be. Do you have any recommendations on how to address this issue?

Thanks in advance!

@yoeunes
Copy link
Owner

yoeunes commented Aug 15, 2024

Hi Reham,

Thanks for the feedback! You’re absolutely right about documenting that step, and I’ll definitely add it when I get the chance. If you’re open to it, it would be awesome if you could contribute by adding this note to the documentation as well.

Regarding the notifications being triggered on each page refresh, it sounds like it might be similar to another issue we’ve seen. Could you take a look at this comment and see if it helps: php-flasher/php-flasher#188 (comment)?

Feel free to reach out if anything else comes up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants