AbdulBazith's avatar

How to use sweet alert in laravel?

guys iam working with a project

iam using flash message in my controller

this is my controller

Session::flash('success','Location Data Entered succesfully');

 return redirect()->route('Location_details.create');

this is my message

@if(Session::has('success'))

<div class="alert alert-success">

<strong>Success: </strong>{{ Session::get('success') }}

</div>

@endif

But now i need to use sweet alert instead of flash messages.

How to use that,

i did this


composer require uxweb/sweet-alert


'providers' => [

    ....

    UxWeb\SweetAlert\SweetAlertServiceProvider::class,

],

'aliases' => [

    ....

    'Alert' => UxWeb\SweetAlert\SweetAlert::class,

]


And atlast i changed my controller to

  Alert::message('Location data entered succesfully!');

            return redirect()->route('Location_details.create');

in the header i added

use Alert

But its not working.

whats the problem how can i use sweet alerts in my controller

Kindly some one suggest

0 likes
13 replies
MThomas's avatar

Did you also install the Sweet Alert JavaScript library trough NPN or Yarn as is stated in the documentation?

Do you get any errors? Did you check the console tab in the Chrome Inspector?

1 like
AbdulBazith's avatar

@MThomas

No i not installed Sweet Alert JavaScript library

because i used


 <link rel=”stylesheet”
                     href=" https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">


 <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>



i added this in my header and footer(javascript)

after iam inserting it just redirecting to another the give page

     Alert::message('Location data entered succesfully!');

            return redirect()->route('Location_details.create');

the return redirect works . but the alert is not working

1 like
AbdulBazith's avatar

@MThomas

One doubt,

actually, iam having a from create.blade which has to fill the details and if i click save it comes to the controller store() method and stores everything, and then these two lines must execute

  Alert::message('Location data entered succesfully!');

            return redirect()->route('Location_details.create');


so whats my doubt is where should i include

@include('sweet::alert')

and where should i add this

<script src="js/sweetalert.min.js"></script>

iam using CDN. so is there any need to install JS library for sweet alert???

1 like
AbdulBazith's avatar

@MThomas

i included

@include('sweet::alert')

But nothing response

why what is the problem

i did everything i think so

else i need to do anything in my createform.blade..??

1 like
MThomas's avatar

It might not sound great, but I think the best ting you could do now is go over the documentation of Sweet Alert and check if you did everything they point out. Often it is just a small thin you missed.

2 likes
AbdulBazith's avatar

@martinbean thankz for your reply,

Noo,, sorry i didnt see.. and sorry for late response not feel well past two days.

Let me try and say what it shows..

AbdulBazith's avatar
AbdulBazith
OP
Best Answer
Level 5

@martinbean and @MThomas thankz for your response it worked.

based on your suggestions i did this

first i included the CDN in my layout.app

and the i added the alert message in my controller

and then i included @include('sweet::alert') this in my view.blade page which i redirected after the adding or deleting process is done..

2 likes
meera's avatar

Hello , i am following this link for sweet alert of laravel Laravel SweetAlert

This is work 100% and easy to know about it

Please or to participate in this conversation.