<?php
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('my-notification/{type}', [HomeController::class, 'myNotification'])->name('notification');
<?php
namespace App\Http\Controllers;
class HomeController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct() {
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index() {
return view('home');
}
/**
* Create a new controller instance.
*
* @return view
*/
public function myNotification($type) {
switch ($type) {
case 'message':
alert()->message('Sweet Alert with message.');
break;
case 'basic':
alert()->basic('Sweet Alert with basic.', 'Basic');
break;
case 'info':
alert()->info('Sweet Alert with info.');
break;
case 'success':
alert()->success('Sweet Alert with success.', 'Welcome to ItSolutionStuff.com')->autoclose(3500);
break;
case 'error':
alert()->error('Sweet Alert with error.');
break;
case 'warning':
alert()->warning('Sweet Alert with warning.');
break;
default:
# code...
break;
}
return view('my-notification');
}
}
How to use sweetalert message box in Laravel?
By Hardik Savani July 14, 2017 Category : PHP Laravel BootstrapPauseUnmuteLoaded: 2.20%FullscreenIn this article, we will learn how to implement sweetalert in laravel 5 application. Here i am going to use uxweb/sweet-alert package for Sweet Alert Messages in laravel application. we can simply use with laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 version.
Sweet Alert is a easy-to-use jquery library and using that we can generate beautiful alert message. Sweet Alert provide good layout and best way to open box with message. you can also use sweetalert for confirmation before delete, few days ago i posted for confirm before delete, you can read here : Laravel 5 - Confirmation before delete record from database example. Here as you can read we can also simply implement sweet alert instead of bootstrap confirmation.
So, In this example i am going to install uxweb/sweet-alert package and then create new route with argument like success, basic, message, info, error and warning. Then i will create new method on controller and create new view for render that. So let's simple follow bellow step, after complete all step you will find prompt box like as bellow screen shot:
Preview:
Step 1: Install uxweb/sweet-alert Package
Here, we will install uxweb/sweet-alert composer package that way we can simply use their method for flash message. So let's run bellow command.
composer require uxweb/sweet-alert
After successfully install package, open config/app.php file and add service provider and alias.
Here, we will add new method myNotification() in HomeController. In this method i use alert() for flash message so let's add following method on your home controller.
app/Http/Controllers/HomeController.php
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class HomeController extends Controller{ /** * Create a new controller instance. * * @return view */ public function myNotification($type) { switch ($type) { case 'message': alert()->message('Sweet Alert with message.'); break; case 'basic': alert()->basic('Sweet Alert with basic.','Basic'); break; case 'info': alert()->info('Sweet Alert with info.'); break; case 'success': alert()->success('Sweet Alert with success.','Welcome to ItSolutionStuff.com')->autoclose(3500); break; case 'error': alert()->error('Sweet Alert with error.'); break; case 'warning': alert()->warning('Sweet Alert with warning.'); break; default: # code... break; } return view('my-notification'); }}
Step 4: Add Blade File
At Last we have to create my-notification.blade.php file and in that file i write code how to use sweetalert package. So let's create blade file and put that code.