PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 17, 2022

[FIXED] How to show a Bootstrap notification / toast (MDBootstrap) after Laravel return an error

 February 17, 2022     bootstrap-4, laravel, laravel-5, mdbootstrap, twitter-bootstrap     No comments   

Issue

I'm doing a small project in the style of Instagram but with basic features using Larvel and MDBootstrap.

Showing my design

I am making the form to edit personal information. Which is valid using Laravel, I want that when Laravel returns an error of the form it shows a "Toast Notification" of error in the page of the form.

These "toast" notifications are triggered by clicking on a button, they have no body in the HTML, they are ready to be used when u call them.

showing "toast notification"

What can I do to detect an error, trigger this "toast" without having to press a button? Thank you


Solution

I understand you need to show a toast if an error is returned by Laravel. Let me propose two solutions.

1. using toastr

In your main template file put this:

    @if (session('error'))
      <script>toastr.error('<?php echo session('error'); ?>')</script>
    @endif

And don't forget to put this in your main js file:

    $('.toast').toast({
        delay:2000,
        // Other options
    });

2. Using Bootstrap native toast

In your main template file put this:

    @if (session('error'))
    <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
      <div class="toast-header">
        <img src="..." class="rounded mr-2" alt="...">
        <strong class="mr-auto">Bootstrap</strong>
        <small class="text-muted">11 mins ago</small>
        <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="toast-body">
        Hello, world! This is a toast message.
      </div>
    </div>
    @endif

And don't forget to put this in your main js file:

$('.toast').toast({})


Answered By - Lucien Dubois
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing