Home > OS >  How to show messages on Laravel 9?
How to show messages on Laravel 9?

Time:12-22

Application doesn't show messages as it should.

I tried returning back with message in RoleController. Redirecting works fine, but I can't see any messages. There is multiple functions like this, none of them show messages.

public function store(Request $request)
    {
        $validated = $request->validate(['name' => ['required', 'min:3']]);
        Role::create($validated);

        return back()->with('message', 'Role created successfully.');
    }

And this code below is in admin.layout blade.

@if (Session::has('message'))
<div  role="alert">
    {{ Session::get('message') }}
    <button type="button"  data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif

I googled this many many times but can't find a solution. This worked fine month ago, but when I continued with this yesterday, it suddenly stopped working. Php version hasn't changed, it's 8.0.2. I also tried flash-messages with no help. Sorry, not a native english speaker.

edit / I have also cleared cache with php artisan.

CodePudding user response:

try this

In Controller

return redirect()->route('admin.listing')->with('message', 'Role created successfully.');

In Blade file

@if (Session::has('message'))
    <div  role="alert">
        {{ Session::get('message') }}
        <button type="button"  data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
@endif



  

CodePudding user response:

I think you can try this...

@if ($message = Session::get('message'))
<div  role="alert">
    <button type="button"  data-bs-dismiss="alert" aria-label="Close"></button>
        <strong>{{ $message }}</strong>
</div>
@endif

CodePudding user response:

Please try one time with this one

return redirect()->back()->with('message', 'Role created successfully.');

CodePudding user response:

Now it's working. I needed to modify Kernel.php and remove \Illuminate\Session\Middleware\StartSession::class, under the protected $middlewareGroups.

  • Related