Laravel Nova 未加载任何资源,blade 错误

Laravel Nova not loading any resources, blade error

Nova 之前为我工作过。我开始在 front-end 上工作,当回到 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

奇怪的是,在 front-end 上,location.blade.php 加载得非常好,因为我在 LocationController 中传递了 $location 变量。没有错误,错误日志中也没有任何内容。在 LocationController:

$location = Location::
  where('id', $this->location_id)
  ->first();

return view('location', [
  'location' => $location
]);

所以它显示了错误,这个错误也在日志中。如果我注释掉 {{ $location->title }},它不再显示错误,但它仍然没有加载任何数据,错误日志中也没有显示任何内容。所以我不知道为什么它没有加载任何数据。为什么 (front-end) Blade 模板会在 Nova 中产生错误,而它在 front-end.

上运行得很好,这对我来说也是一个谜

更新:

如果我在 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 控制器。)

您可以通过从 app/Providers/NovaServiceProvider.php 文件中取出 Nova::routes 调用并将其直接放入您的路由文件来解决此问题,但更简洁的解决方案可能会将您的路由调整为像 /locations/{location_id}/{location_title} 这样不会发生冲突。通配符顶级路由往往会导致这样的问题。

您也可以这样做:

Route::get('/{location_id}/{location_title}', 'LocationController@viewLocation')
   ->where('location_id', '[0-9]+');

这将使您的路线仅针对数字 ID 激活,这意味着它不会干扰非数字 nova-api 路线。