skip to Main Content

I was to trying to show flash message with following controller. But it’s not displaying any message on view page.

public function deleteTag(Request $request)
    {
        DB::table('tags')
            ->where('id', $request->id)
            ->where('user_id', Auth::user()->id)
            ->delete();
        Session::flash('error', 'Tag deleted successfully' );
        return redirect('tags');
    }

In view part:

   @if (Session::has('error'))
        <div class="alert alert-primary inline-form-alert d-flex align-items-center justify-content-between table-alert">
            <ul>
                <li class="alert-icon-content"><i class="bi bi-exclamation-circle-fill me-2"></i>{{ Session('error') }}</li>
            </ul> 
           <button class="btn-close ms-3" type="button" data-bs-dismiss="alert" aria-label="Close"></button>
         </div>
     @endif

In same page following controller flash message is showing. Why flash message not displaying on delete controller?

public function storeTag(Request $request)
    {
        $this->validate($request, [
            'tag_name' => [
                Rule::unique('tags')->where(function ($query) {
                    $query->where('user_id', '=', Auth::user()->id);
                })
            ],
        ]);

        $tag = new Tag();
        $tag->tag_name = $request->Input(['tag_name']);
        $tag->user_id = Auth::user()->id;
        $tag->save();

        Session::flash('error', 'Tag created successfully' );

        return redirect('tags');
    }

2

Answers


  1. Here is the Docs Link

    You can redirect to route with the flash message.

    You need to check first that if data is delete successful if yes then send success otherwise send error

    public function deleteTag(Request $request)
        {
            $result = DB::table('tags')
                ->where('id', $request->id)
                ->where('user_id', Auth::user()->id)
                ->delete();
            if($result == true){
                 return redirect('tags')->with('success', 'Tag deleted successfully');
            }else{
                return redirect('tags')->with('error', 'Something Went Wrong');
            }
        } 
    

    In the view section you can add this code and one thing you must be check that this code is need to write where the redirect('tags') route load the view.

        @if (session('success'))
                <div class="alert alert-primary inline-form-alert d-flex align-items-center justify-content-between table-alert">
                    <ul>
                        <li class="alert-icon-content"><i class="bi bi-exclamation-circle-fill me-2"></i>{{ session('success') }}</li>
                    </ul> 
                   <button class="btn-close ms-3" type="button" data-bs-dismiss="alert" aria-label="Close"></button>
                 </div>
             @elseif (session('error'))
                 <div class="alert alert-primary inline-form-alert d-flex align-items-center justify-content-between table-alert">
                    <ul>
                        <li class="alert-icon-content"><i class="bi bi-exclamation-circle-fill me-2"></i>{{ session('error') }}</li>
                    </ul> 
                   <button class="btn-close ms-3" type="button" data-bs-dismiss="alert" aria-label="Close"></button>
                 </div>
        @endif
    
    Login or Signup to reply.
  2. Make sure that this line LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class, stays commented or removed in the Kernel.php

    protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            IlluminateSessionMiddlewareStartSession::class,
            IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,
            // LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class,
    
        ],
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search