gpt4 book ai didi

php - HasRelationships.php 中的 ErrorException

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:34:29 24 4
gpt4 key购买 nike

我收到这个错误,但我不确定是因为关系还是其他原因?

错误

ErrorException in HasRelationships.php line 487:
Class 'Company' not found

用户.php

公共(public)函数 company(){
$this->belongsTo('公司', 'user_id');
}

公司.php

公共(public)函数用户(){
$this->belongsTo('用户') ;
}

现在我的目标是如果用户与公司表没有关系,则隐藏导航栏中的“创建列表”按钮。我知道我可以用角色或中间件来实现,但我的 friend 给我发了类似的东西,并告诉我这样做更容易。

if(count($user->company) > 0) 

所以现在我想弄清楚如何,但仍然无法弄清楚如何修复错误。

导航 View

@inject('user', 'App\User')
@if(count($user->company) > 0)
<li><a href="{{route('listings.create', [$area])}}">Add listing</a></li>
@endif

///更新

它没有找到“公司”类,因为我没有在我的关系中使用完整的命名空间,但现在我收到了这个新错误。

错误

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)

最佳答案

在关系代码中使用完整的命名空间:

public function company()
{
return $this->belongsTo('App\Company', 'user_id');
}

关于php - HasRelationships.php 中的 ErrorException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com