SigalZ's avatar

SigalZ started a new conversation+100 XP

4mos ago

I am trying to use realrashid/sweetalert2 in laravel 12.

I installed the package and in my blade I have this code:

<!DOCTYPE html>
<html lang="en">
<head>
    @include('layouts.meta')

    <title>Home | {{ config('app.name') }}</title>

    @vite(['resources/sass/app.scss', 'resources/js/app.js'])
</head>
<body data-bs-spy="scroll" data-bs-target="#navbarNav" data-bs-offset="80" tabindex="0">
... some html...

@include('sweetalert::alert')   
   
    <script src="https://kit.fontawesome.com/5fb9c0ddb1.js" crossorigin="anonymous"></script>
    <script src="https://snapwidget.com/js/snapwidget.js"></script>
       
</body>

In my app.js:

import './bootstrap';

import Swal from 'sweetalert2'

window.Swal = Swal

I get the error:

GET http://highlandnew.local/vendor/sweetalert/sweetalert.all.js [HTTP/1.1 404 Not Found 136ms]

Uncaught ReferenceError: Swal is not defined

I looked at: vendor/realrashid/sweet-alert/resources/views/alert.blade.php it has this line:

<script src="{{ $cdn ?? asset('vendor/sweetalert/sweetalert.all.js') }}"></script>

There is no folder vendor/sweetalert, there is folder: vendor/realrashid/sweet-alert...

So I published the package assets, and now I have:

resources\views\vendor\sweetalert\alert.blade.php and public\vendor\sweetalert\sweetalert.all.js

but now, this line:

@include('sweetalert::alert') 

does not include the alert view in the blade, and of course I'm getting the Swal not defined error again.

What am I missing?

SigalZ's avatar

SigalZ liked a comment+100 XP

5mos ago

Nothing goes to an admin route; you're crashing the app when Laravel is trying to register routes:

dd('admin');

Add the route definitions instead of a dd() call.

SigalZ's avatar

SigalZ wrote a reply+100 XP

5mos ago

Ok, I found a way to make it all work.

In the app.php file I add the admin.php route file into the web route:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: [
            __DIR__ . '/../routes/web.php',
            __DIR__ . '/../routes/admin.php'
        ],

And in my admin.php I removed the dd('admin') which caused errors and put a route call to a controller:

Route::middleware(AdminMiddlware::class)->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminController::class, 'index'])->name('dashboard');
});
SigalZ's avatar

SigalZ wrote a reply+100 XP

5mos ago

Thank you.

SigalZ's avatar

SigalZ wrote a reply+100 XP

5mos ago

Thank you.

I tried your second solution so now my web.php has this code:

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PagesController;

Auth::routes();

require __DIR__ . '/admin.php';

Route::get('/', [HomeController::class, 'index'])->name('home');

My admin.php file:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Middleware\AdminMiddlware;

Route::middleware(AdminMiddlware::class)->prefix('admin')->name('admin.')->group(function () {
    dd('admin');
})->name('dashboard');

app.php

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
        /*then: function () {

            Route::middleware(AdminMiddlware::class)
                ->prefix('admin')
                ->name('adminn.')
                ->group(base_path('routes/admin.php'));
        },*/

    )
    ->withMiddleware(function (Middleware $middleware): void {
        //
        $middleware->append(ProtectAgainstSpam::class);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();

Now I can't browse anywhere besides the admin dashboard, e.g. if I put the sites home page in the browser's address, it still goes to the admin route instead of the home page and I get this error on VS code:

2025-11-24 11:24:00.382 [info] Laravel Extra Intellisense command started: Application Models 2025-11-24 11:24:00.383 [info] Laravel Extra Intellisense command started: Auth Data 2025-11-24 11:24:02.053 [error] Laravel Extra Intellisense Error: Auth Data

"admin" // routes\admin.php:14

2025-11-24 11:24:02.059 [error] Laravel Extra Intellisense Error: Application Models

"admin" // routes\admin.php:14

Can you please help?

SigalZ's avatar

SigalZ started a new conversation+100 XP

5mos ago

Using Laravel 12, I have a custom routes file.

All the routes in this file should be blocked to guests and users that don't have a specific permission.

I can't make the middlware work correctly.

The user I am testing has the permission 'admin dashboard' through the 'admin' role.

The routes file: routes/admin.php:

use Illuminate\Support\Facades\Route;

Route::get('/dashboard', function () {
dd('admin');
})->name('dashboard');

In bootstrap/app.php:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
        then: function () {

            Route::middleware( 'admin')

                ->prefix('admin')

                ->name('admin.')

                ->group(base_path('routes/admin.php'));
        },

    )
    ->withMiddleware(function (Middleware $middleware): void {
        //
        $middleware->append(ProtectAgainstSpam::class);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();

In App/Http/Middleware/AdminMiddleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Auth;

class AdminMiddlware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
//Tried
        if (! $request->user() || ! $request->user()->can('admin dashboard')) {
//And Tried
		if (! Auth::user() || ! Auth::user()->can('admin dashboard')) {
            abort(403, 'Unauthorized action.'); // Or redirect to a different page
        }
		
        return $next($request);
    }
}

Even though the user is logged in and has the permission, I get the Unauthorized page.

using: dd(Auth::user()) or dd($request->user()) both return null.

Can anyone help please?