Php 如何解决此错误:explode()希望参数2为字符串,对象为给定的?

Php 如何解决此错误:explode()希望参数2为字符串,对象为给定的?,php,laravel,inertiajs,Php,Laravel,Inertiajs,我试图在我的项目中的AppServiceProvider.php中设置不同的用户类型及其各自的权限,但我得到了错误 explode()要求参数2为字符串,对象为给定对象 我的代码中没有一个explode()至少我能看到。在添加惯性::共享(函数(){})之前,没有这样的错误 这是我的代码: public function register() { Inertia::version(function () { return md5_file(public_path('mi

我试图在我的项目中的
AppServiceProvider.php
中设置不同的用户类型及其各自的权限,但我得到了错误
explode()要求参数2为字符串,对象为给定对象

我的代码中没有一个
explode()
至少我能看到。在添加
惯性::共享(函数(){})
之前,没有这样的错误

这是我的代码:

public function register()
{

    Inertia::version(function () {
        return md5_file(public_path('mix-manifest.json'));
    });

    Inertia::share(function () {
       $auth = null;
       if (Auth::user()) {
           $perms = [];
           $user = Auth::user();

           if ($user->isSuperAdmin() || $user->isAdmin()) {
               $perms = [
                   [
                       'url' => '/',
                       'icon' => 'fa fa-home',
                       'name' => 'Dashboard'
                   ],
                   [
                       //rest of permissions
                   ],
               ];
           }
           if ($user->isUser()) {
               $perms = [
                   [
                       'url' => '/',
                       'icon' => 'fa fa-home',
                       'name' => 'Dashboard'
                   ],
                   [
                       //rest of permissions
                   ],
               ];
           }

           $auth = [
               'id' => Auth::user()->id,
               'name' => Auth::user()->name,
               'card' => Auth::user()->card,
               'scard' => Auth::user()->scard,
               'user_type_id' => Auth::user()->user_type_id,
               'email' => Auth::user()->email,
               'perms' => $perms
           ];
       }
       return [
           'app' => [
               'name' => Config::get('app.name'),
           ],
           'auth' => [
               'user' => $auth,
           ],
           'flash' => [
               'success' => Session::get('success'),
           ],
           'errors' => Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object)[],
    ]
});


我做错了什么?在我得到错误的地方,它没有指明错误在哪里,只是指明了错误是什么,它发出了我给出的代码的最后一行错误在哪里的信号,但所有这些都是右括号和括号。

对惯性一无所知,似乎你误用了
惯性::share
函数。在他们的文章中,我看到了3个例子。前两个参数1是一个字符串(例如,
'auth.user'
'app.name'
),最后一个参数1是一个关联数组,因此每个元素仍然有一个唯一的字符串键

在代码中,传递闭包作为第一个参数。我相信您可以通过简单地添加一个名称作为第一个参数来修复它:

Inertia::share('auth.user', function () {
    $auth = null;
    if (Auth::user()) {
        $perms = [];
        $user = Auth::user();

        if ($user->isSuperAdmin() || $user->isAdmin()) {
            $perms = [
                [
                    'url' => '/',
                    'icon' => 'fa fa-home',
                    'name' => 'Dashboard'
                ],
                [
                    //rest of permissions
                ],
            ];
        }
        if ($user->isUser()) {
            $perms = [
                [
                    'url' => '/',
                    'icon' => 'fa fa-home',
                    'name' => 'Dashboard'
                ],
                [
                    //rest of permissions
                ],
            ];
        }

        $auth = [
            'id' => Auth::user()->id,
            'name' => Auth::user()->name,
            'card' => Auth::user()->card,
            'scard' => Auth::user()->scard,
            'user_type_id' => Auth::user()->user_type_id,
            'email' => Auth::user()->email,
            'perms' => $perms
        ];
    }
    return [
        'app' => [
            'name' => Config::get('app.name'),
        ],
        'auth' => [
            'user' => $auth,
        ],
        'flash' => [
            'success' => Session::get('success'),
        ],
        'errors' => Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object)[],
    ];
});