将 Laravel 从 5.6 升级到 6.0 后,调用未定义的 str_random() 函数不起作用

After upgrading Laravel from 5.6 to 6.0, Call to undefined str_random() function not working

我已将 Laravel 从 5.6 升级到 6.0。以前,默认辅助函数在控制器上 运行ning 正常,但现在它显示“未定义”。在我的控制器中,我使用了以下内容。

$filename = str_random(12);

我收到以下错误。

message: "Call to undefined function App\Http\Controllers\str_random()"

我也用过random()函数,也是一样的意思

有人可以指导我该怎么做吗?

我有 运行 个命令,例如:

composer dump-autoload

但是我得到了同样的错误。

Likelihood Of Impact: High Laravel 6 Upgrade Guide

在 Laravel 6 中,所有 str_array_ 助手都已移至新的 laravel/helpers Composer 包并从框架中删除.如果需要,您可以更新对这些助手的所有调用以使用 Illuminate\Support\StrIlluminate\Support\Arr 类。或者,您可以将新的 laravel/helpers 包添加到您的应用程序以继续使用这些助手:

composer require laravel/helpers

如果不想加包就用StrArr类.

例如:

Str::random(12)

https://laravel.com/docs/master/helpers#method-str-random

添加以下字符串库。

use Illuminate\Support\Str;

现在您可以使用它了。

$filename = Str::random(40)

或者,安装以下软件包。

composer require laravel/helpers

在我的例子中,我没有在我的应用程序代码中使用任何字符串助手,所以我只需要删除编译的 class 文件:

php artisan clear-compiled

使用代码::

<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;



   public function index()
    {
        $count=15;
        try {
            DB::statement('truncate users');
            DB::beginTransaction();
            while ($count--){
                $id = DB::table('users')->insertGetId( [
                    'name'=>'Sample'.$count,
                    'password'=>random_int(1000000,99999999)
                ]);
                foreach (range(1,rand(1,3)) as $index ){
                    DB::insert('INSERT INTO posts (userid,title,body) VALUES (:userid,:title,:body)',[
                            'userid'=>$id,
                            'title'=>str::random(15),
                            'body'=>str::random(50),
                        ]);
                }
                DB::commit();
            }
        }catch (\Exception $errors){
            DB::rollBack();
            Log::error($errors);
            return "mission filed";
        }
    }