Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

str_slug() and the other str_ and array_ helpers are gone after upgrading Laravel

ErrorCall to undefined function str_slug()

Short answer

Laravel 6 removed every global str_ and array_ helper from the framework, so str_slug() does not exist in Laravel 12. Replace each call with the Illuminate\Support\Str class, here Str::slug(). If a package you cannot edit still calls the old function, install laravel/helpers, which brings the old helpers back.

  • Applies to Laravel 12, PHP 8.3, Composer 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every call of the removed helpers, first in your own code and then in the packages. A stack trace that ends in vendor/ points at a package. One that ends in app/ or a Blade view points at your code.

    grep -rn "str_slug(" app/ resources/ routes/ database/ config/
    grep -rl "str_slug(" vendor/ --include=*.php
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace ends in app/, routes/, database/ or a Blade viewYour own code still calls str_slug() or another removed helperFix
    2. 2The stack trace ends in a file under vendor/, and your own code has no str_slug() callA package in vendor/ calls the removed helperFix

What does the error look like?

  • Call to undefined function str_slug()
  • Call to undefined function array_get()

Cause 1 of 2

Your own code still calls str_slug() or another removed helper

1Check

The Laravel 6 upgrade guide says all str_ and array_ helpers were moved to the laravel/helpers package and removed from the framework. A call that comes from your code has to change, or be covered by that package.

grep -rnE "str_slug\(|array_get\(" app/ resources/views/ routes/ database/

2Fix

Use the Str and Arr classes the guide names. For slugs that is Str::slug(). In a Blade view, write the full class name, or use the str() helper and its slug method.

use Illuminate\Support\Str;

// before
$slug = str_slug($title, '-');
$value = array_get($data, 'user.name');

// after
$slug = Str::slug($title, '-');
$value = \Illuminate\Support\Arr::get($data, 'user.name');

// in Blade, without an import
{{ \Illuminate\Support\Str::slug($post->title) }}

// fluent form
$slug = str($title)->slug('-');

3Verify

Run the page or command that failed. The slug comes out in lower case with hyphens.

// in php artisan tinker
>>> Illuminate\Support\Str::slug('Laravel 5 Framework', '-');
=> "laravel-5-framework"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

A package in vendor/ calls the removed helper

1Check

A package written for an older Laravel may still call str_slug() or array_get(). The grep over vendor/ in the first step lists those files.

grep -rl "str_slug(" vendor/ --include=*.php

2Fix

Update the package to a version that supports your Laravel version. If none exists, install laravel/helpers, a compatibility layer for the old helpers, and your code and the package both work again.

composer require laravel/helpers

3Verify

Composer lists the package, and the request that failed now works.

composer show laravel/helpers

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Prefer the Str and Arr classes in new code, so no upgrade ever depends on global helper functions.
  • Before upgrading, search for str_ and array_ calls and replace them in the same change as the framework bump.
  • Run the test suite and the main pages after each major Laravel upgrade, because an undefined function only fails when its line runs.
  • Add laravel/helpers only as a stopgap and remove it once the code and its packages no longer need it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.