Nova以前为我工作。我开始在前端工作,当我回到Nova时,它突然不再工作了。我可以登录,但它显示了所有资源的加载动画,而不是加载数据。
我得到这个错误:
Trying to get property of non-object (View: longpath/location.blade.php)
在location.blade.php
中
@extends('app')
@section('title')
{{ $location->title }}
@endsection
@section('content')
@endsection
奇怪的是,在前端,location.blade.php加载得非常好,因为我在LocationController
中传递了$location
变量。没有错误,错误日志中也没有任何内容。在LocationController
:中
$location = Location::
where('id', $this->location_id)
->first();
return view('location', [
'location' => $location
]);
所以它显示了错误,这个错误也在日志中。如果我注释掉{{ $location->title }}
,它不会再显示错误,但它仍然没有加载任何数据,错误日志中也不会显示任何内容。所以我不知道为什么它没有加载任何数据。对我来说,这也是一个谜,为什么(前端(刀片模板会在Nova中生成错误,而它在前端运行得非常好。
更新:
如果我在routes/web中评论出这个特定的路线,Nova会再次工作。不确定这条路线为什么会影响Nova?
Route::get('/{location_id}/{location_title}', 'LocationController@viewLocation');
如果我添加返回的路线,在我的控制台中我会得到:
TypeError: Cannot read property 'length' of undefined
您的路线有问题,因为:
Route::get('/{location_id}/{location_title}', 'LocationController@viewLocation');
将捕获任何/foo/bar
URL。
如果你做php artisan route:list | grep nova
,你会看到Nova的所有路线,你会发现一堆这种格式的:
/nova-api/metrics
/nova-api/cards
/nova-api/search
/nova-api/{resource}
等。等等。
(换句话说,Nova的一系列路线被发送到您的LocationController
,而不是正确的Nova控制器。(
您可以通过将Nova::routes
调用从app/Providers/NovaServiceProvider.php
文件中取出并直接放入路由文件来解决此问题,但更干净的解决方案可能会将路由调整为类似/locations/{location_id}/{location_title}
的不会发生冲突的路由。使用通配符的顶级路由往往会导致这样的问题。
你也可以这样做:
Route::get('/{location_id}/{location_title}', 'LocationController@viewLocation')
->where('location_id', '[0-9]+');
这将使您的路线仅为数字ID激活,这意味着它不会干扰非数字nova-api
路线。