jqhph / dcat-admin

🔥 基于 Laravel 的后台系统构建工具 (Laravel Admin),使用很少的代码快速构建一个功能完善的高颜值后台系统,内置丰富的后台常用组件,开箱即用,让开发者告别冗杂的HTML代码
http://www.dcatadmin.com
MIT License
3.9k stars 713 forks source link

$grid->name->tree() 没有数据时点击展开,报500错误 #1299

Open wangdeer opened 3 years ago

wangdeer commented 3 years ago

Description:

2.0.24-beta 升级到 2.1.0-beat 以后

$grid->name->tree() 没有关联数据时,点击展开报 500 错误:

image

Error: Method Dcat\Admin\Grid::__toString() must return a string value in file /var/www/xintead/vendor/dcat/laravel-admin/src/Support/Helper.php on line 108

#0 /var/www/xintead/vendor/dcat/laravel-admin/src/Layout/Column.php(107): Dcat\Admin\Support\Helper::render(Object(Dcat\Admin\Grid))
#1 /var/www/xintead/vendor/dcat/laravel-admin/src/Layout/Row.php(75): Dcat\Admin\Layout\Column->render()
#2 /var/www/xintead/vendor/dcat/laravel-admin/src/Layout/Content.php(281): Dcat\Admin\Layout\Row->render()
#3 /var/www/xintead/vendor/dcat/laravel-admin/src/Layout/Content.php(534): Dcat\Admin\Layout\Content->build()
#4 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Http/Response.php(69): Dcat\Admin\Layout\Content->render()
#5 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Http/Response.php(35): Illuminate\Http\Response->setContent(Object(Dcat\Admin\Layout\Content))
#6 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Routing/Router.php(793): Illuminate\Http\Response->__construct(Object(Dcat\Admin\Layout\Content), 200, Array)
#7 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Routing/Router.php(763): Illuminate\Routing\Router::toResponse(Object(Illuminate\Http\Request), Object(Dcat\Admin\Layout\Content))
#8 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Routing/Router.php(695): Illuminate\Routing\Router->prepareResponse(Object(Illuminate\Http\Request), Object(Dcat\Admin\Layout\Content))
#9 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#10 /var/www/xintead/vendor/dcat/laravel-admin/src/Http/Middleware/WebUploader.php(22): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dcat\Admin\Http\Middleware\WebUploader->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 /var/www/xintead/vendor/dcat/laravel-admin/src/Http/Middleware/Session.php(12): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dcat\Admin\Http\Middleware\Session->handle(Object(Illuminate\Http\Request), Object(Closure))
#14 /var/www/xintead/vendor/dcat/laravel-admin/src/Http/Middleware/Permission.php(40): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dcat\Admin\Http\Middleware\Permission->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 /var/www/xintead/vendor/dcat/laravel-admin/src/Http/Middleware/Bootstrap.php(19): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dcat\Admin\Http\Middleware\Bootstrap->handle(Object(Illuminate\Http\Request), Object(Closure))
#18 /var/www/xintead/vendor/dcat/laravel-admin/src/Http/Middleware/Pjax.php(23): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dcat\Admin\Http\Middleware\Pjax->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 /var/www/xintead/vendor/dcat/laravel-admin/src/Http/Middleware/Authenticate.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dcat\Admin\Http\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))
#22 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#24 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#25 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 /var/www/xintead/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#27 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#28 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Session/Middleware/AuthenticateSession.php(39): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#29 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Session\Middleware\AuthenticateSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#30 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\Session\Middleware\StartSession->handleStatefulRequest(Object(Illuminate\Http\Request), Object(Illuminate\Session\Store), Object(Closure))
#32 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#33 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#37 /var/www/xintead/vendor/dcat/laravel-admin/src/Http/Middleware/Application.php(15): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#38 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Dcat\Admin\Http\Middleware\Application->handle(Object(Illuminate\Http\Request), Object(Closure), 'admin')
#39 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#40 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Routing/Router.php(697): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#41 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Routing/Router.php(672): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#42 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#43 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#44 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#45 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#46 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#47 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#48 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull->handle(Object(Illuminate\Http\Request), Object(Closure))
#49 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#50 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(40): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#51 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TrimStrings->handle(Object(Illuminate\Http\Request), Object(Closure))
#52 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#53 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#54 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#55 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))
#56 /var/www/xintead/vendor/fruitcake/laravel-cors/src/HandleCors.php(38): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#57 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#58 /var/www/xintead/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#59 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#60 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#61 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#62 /var/www/xintead/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#63 /var/www/xintead/public/index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#64 {main}
    protected function grid()
    {
        return Grid::make(new Category(), function (Grid $grid) {
            // 筛选
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('type', \App\Models\Setting::query()->where('key', 'category_type')->first()->value ?? []);
            });

            // 操作
            $grid->disableRowSelector();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableView();
            });
            $grid->showQuickEditButton();

            $grid->column('id')->sortable();
            $grid->name->tree(); // 开启树状表格功能
            $grid->column('name_en');
            $grid->column('icon')->image();
            $grid->column('show')->switch();
            $grid->column('order')->sortable()->editable(true);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        });
    }

经过调试发现 Method Dcat\Admin\Grid::__toString() 中的 $this->render(); 在没有关联数据时返回的是 null

Steps To Reproduce:

attentive20 commented 3 years ago

I have the same situation. If there is no associated data, an error occurs. If there is - everything is ok. 2.1.0-beta

wangdeer commented 3 years ago

@attentive20

临时解决方案是把 /vendor/dcat/laravel-admin/src/Grid.php 中的

    public function __toString()
    {
        return $this->render();
    }

改为:

    public function __toString()
    {
        return $this->render() ?? '';
    }

然后等新版本修复。