S4b*_*eur 5 php relationship laravel eloquent
即时通讯收到此错误,但我不确定是由于关系还是其他原因?
错误
ErrorException in HasRelationships.php line 487:
Class 'Company' not found
Run Code Online (Sandbox Code Playgroud)
User.php
public function company(){
$this->belongsTo('Company', 'user_id');
}
Company.php
public function user(){
$this->belongsTo('User') ;
}
现在,我的目标是在导航栏中隐藏“创建列表”按钮(如果用户与companies表没有关系)。我知道我可以使用角色或中间件来实现,但是我的朋友给我发送了类似的信息,并告诉我它更容易实现。
if(count($user->company) > 0)
Run Code Online (Sandbox Code Playgroud)
因此,现在我试图找出方法,但仍然无法弄清楚如何解决该错误。
导航视图
@inject('user', 'App\User')
@if(count($user->company) > 0)
<li><a href="{{route('listings.create', [$area])}}">Add listing</a></li>
@endif
Run Code Online (Sandbox Code Playgroud)
///更新
它没有找到类'Company',因为我没有在关系中使用完整的名称空间,但是现在我遇到了这个新错误。
错误
ErrorException in HasAttributes.php line 403:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
(View: /Users/username/Desktop/laravel/resources/views/layouts/partials/_navigation.blade.php)
(View: /Users/username/Desktop/laravel/resources/views/layouts/partials/_navigation.blade.php)
(View: /Users/username/Desktop/laravel/resources/views/layouts/partials/_navigation.blade.php)
Run Code Online (Sandbox Code Playgroud)
在关系代码中使用完整的名称空间:
public function company()
{
return $this->belongsTo('App\Company', 'user_id');
}
Run Code Online (Sandbox Code Playgroud)