【问题标题】:How to solve this error: explode() expects parameter 2 to be string, object given?如何解决这个错误:explode() 期望参数 2 是字符串,给定对象?
【发布时间】:2019-10-01 20:03:59
【问题描述】:

我试图在我的项目中的AppServiceProvider.php 中设置不同的用户类型及其各自的权限,但我收到错误
explode() expects parameter 2 to be string, object given

至少在我的代码中我没有看到explode()。在添加Inertia::share(function(){})之前没有这样的错误。

这是我的代码:

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)[],
    ]
});

我做错了什么?我在哪里得到错误,它没有指定错误在哪里,只是它是什么,它表明我提供的代码的最后一行是错误在哪里,但所有这些都是右括号和括号。

【问题讨论】:

    标签: php laravel inertiajs


    【解决方案1】:

    对 Inertia 一无所知,您似乎在滥用Inertia::share 函数。在他们的docs 中,我看到了 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)[],
        ];
    });
    

    【讨论】:

      猜你喜欢
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2016-06-26
      • 2017-01-02
      • 2021-02-13
      • 1970-01-01
      相关资源
      最近更新 更多