Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AungHtetPaing__'s avatar

trim(): Passing null to parameter #1 ($string) of type string is deprecated

When I run php artisan route:list "trim():passing null to parameter" error happen on route prefix('admin'). No error when I remove route prefix. Currently I am using separate admin route file like auth.php.

// web.php
<?php

use App\Http\Controllers\AddressController;
use App\Http\Controllers\BookController;
use App\Http\Controllers\BuyNowController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\CouponController;
use App\Http\Controllers\ProfilePasswordController;
use App\Http\Controllers\RedirectToHomeController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SaveForLaterController;
use App\Http\Controllers\SearchController;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

//dd(User::find(2)->addresses);

Route::get('/', function () {
    return Inertia::render('Welcome', [
        'canLogin' => Route::has('login'),
        'canRegister' => Route::has('register'),
    ]);
})->name('welcome');

/** give conditional redirect path after login successfully */
Route::get('home', RedirectToHomeController::class)->name('home');
Route::get('search', SearchController::class)->name('search');

Route::get('books', [BookController::class, 'index'])->name('books.index');
Route::get('books/{book:slug}', [BookController::class, 'show'])->name('books.show');

Route::get('carts', [CartController::class, 'index'])->name('cart.index');
Route::post('carts/{book}', [CartController::class, 'store'])->name('cart.store');
Route::patch('carts/{book}', [CartController::class, 'update'])->name('cart.update');
Route::delete('carts/{book}', [CartController::class, 'destroy'])->name('cart.destroy');

Route::post('buyNow/{book}', [BuyNowController::class, 'store'])->name('buyNow.store');
Route::patch('buyNow/{book}', [BuyNowController::class, 'update'])->name('buyNow.update');
Route::delete('buyNow/{book}', [BuyNowController::class, 'destroy'])->name('buyNow.destroy');

Route::group(['middleware' => ['auth', 'verified']], function () {
    Route::post('save-for-later/{id}', [SaveForLaterController::class, 'store'])->name('saveForLater.store');
    Route::post('move-to-cart/{id}', [SaveForLaterController::class, 'moveToCart'])->name('moveToCart');

    Route::get('profile/{user:name}', [ProfileController::class, 'show'])->name('profile.show');
    Route::get('settings/account', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('settings/account', [ProfileController::class, 'update'])->name('profile.update');

    Route::patch('profile-password', [ProfilePasswordController::class, 'update'])->name('profile-password.update');

    Route::get('checkout', [CheckoutController::class, 'index'])->name('checkout.index');

    Route::get('orders', [OrderController::class, 'index'])->name('orders.index');
    Route::get('orders/{order}', [OrderController::class, 'show'])->name('orders.show');
    Route::post('orders', [OrderController::class, 'store'])->name('orders.store');

    Route::get('coupons', [CouponController::class, 'index'])->name('coupons.index');
    Route::post('coupons', [CouponController::class, 'store'])->name('coupons.store');
    Route::delete('coupons', [CouponController::class, 'destroy'])->name('coupons.destroy');

    Route::get('addresses', [AddressController::class, 'index'])->name('addresses.index');
    Route::post('addresses', [AddressController::class, 'store'])->name('addresses.store');
    Route::patch('addresses/{address}', [AddressController::class, 'update'])->name('addresses.update');
});

Route::get('thank-you/{order}', [CheckoutController::class, 'thankYou'])->name('checkout.thankYou');


Route::get('login-as-admin', function (){
    abort_if(config('app.env') !== 'local',405);

    Auth::loginUsingId(1);

    return redirect()->route('admin.dashboard');
})->name('loginAsAdmin');

Route::get('login-as-user', function (){
    abort_if(config('app.env') !== 'local',405);

    Auth::loginUsingId(2);

    return redirect()->route('profile.show', User::find(2)->name);
})->name('loginAsUser');

Route::get('mailable', function () {
    $order = App\Models\Order::find(1);

    return new App\Mail\OrderStatusUpdated($order);
});

require __DIR__ . '/auth.php';
require __DIR__ . '/admin.php';

I tried to solve that error by updating RouteServiceProvider.php like below and removed "require" line from web.php

Route::prefix('admin')
                ->namespace($this->namespace)
                ->group(base_path('routes/admin.php'));

that solved "trim(): Passing null" error but it make "redirect loop" error when I login as admin.

redirect loop between three route when I check network tab

login-as-admin | 302 | app.js
dashboard | 302 | login-as-admin
login | 302 | dashboard
home | 302 | login
dashboard | 302 | home
login | 302 | dashboard
etc....
// admin.php
<?php

use App\Http\Controllers\Admin\BookController;
use App\Http\Controllers\Admin\CouponController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\NotificationController;
use App\Http\Controllers\Admin\OrderController;
use Illuminate\Support\Facades\Route;

Route::prefix('admin')->group([
    // 'prefix' => 'admin',
    'middleware' => ['auth', 'admin']
], function () {
    Route::get('dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');

    Route::get('notifications', [NotificationController::class, 'index'])->name('admin.notifications.index');
    Route::get('unread-notifications', [NotificationController::class, 'unreadNotifications'])->name('admin.unread-notifications');
    Route::get('notifications/{notification}', [NotificationController::class, 'show'])->name('admin.notifications.show');

    Route::get('books', [BookController::class, 'index'])->name('admin.books.index');
    Route::get('books/create', [BookController::class, 'create'])->name('admin.books.create');
    Route::get('books/{book}/edit', [BookController::class, 'edit'])->name('admin.books.edit');
    Route::post('books', [BookController::class, 'store'])->name('admin.books.store');
    Route::patch('books/{book}', [BookController::class, 'update'])->name('admin.books.update');
    Route::delete('books/{book}', [BookController::class, 'destroy'])->name('admin.books.destroy');
    // Route::get('/books/{book}', [BookController::class, 'show'])->name('admin.books.show');

    Route::get('orders', [OrderController::class, 'index'])->name('admin.orders.index');
    Route::get('orders/{order}', [OrderController::class, 'show'])->name('admin.orders.show');
    Route::get('orders/{order}/edit', [OrderController::class, 'edit'])->name('admin.orders.edit');
    Route::patch('orders/{order}', [OrderController::class, 'update'])->name('admin.orders.update');

    Route::get('coupons', [CouponController::class, 'index'])->name('admin.coupons.index');
    Route::post('coupons', [CouponController::class, 'store'])->name('admin.coupons.store');
    Route::get('coupons/create', [CouponController::class, 'create'])->name('admin.coupons.create');
    Route::get('coupons/{coupon}/edit', [CouponController::class, 'edit'])->name('admin.coupons.edit');
    Route::patch('coupons/{coupon}', [CouponController::class, 'update'])->name('admin.coupons.update');
});
// admin middleware
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class Administrator
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (auth()->user()->role !== 'admin') {
            abort(Response::HTTP_FORBIDDEN);
        }

        return $next($request);
    }
}

I still can't find solution for redirect loop error. When I search on google about "trim(): passing null" error I found result with php 8.1 so is it happen with php 8.1? My php version is 8.1.11.

Any Advice for this problem? Thanks.

0 likes
5 replies
Snapey's avatar

i think using require in web.php might break route caching

so are you adding auth as a new file to be processed in Route service provider? I cant tell from the three lines shown?

AungHtetPaing__'s avatar

@Snapey

I cant tell from the three lines shown?

Ah sorry about that below is full routeserviceprovider

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    // protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            Route::prefix('admin')
                ->name('admin.')
                ->namespace($this->namespace)
                ->group(base_path('routes/admin.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}

what laravel version?

Laravel version in composer is 8.54

Snapey's avatar
Snapey
Best Answer
Level 122

@AungHtetPaing__ you are missing web middleware?

This will cause you not to be logged in, which is probably creating the redirect loop

1 like
AungHtetPaing__'s avatar

@Snapey Thank you so much. I forget about that in RouteServiceProvider and looking at "auth" middleware in admin.php. HAHA

Please or to participate in this conversation.