S4boteur
S4boteur

Reputation: 145

ErrorException in HasRelationships.php

Im gettings this error, but i'm not sure is it because of the relationship or something else?

Error

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

User.php

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

Company.php

public function user(){ $this->belongsTo('User') ; }

Now my goal is to hide "Create Listing" button in navigation bar, if user doesn't have relation with companies table. I know i can make it with roles or middleware, but my friend send me something like that and told me its easier to make that way.

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

So now i'm trying to figure out how, but still can't figure out how to fix the error.

Navigation view

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

///UPDATE

It didn't find class 'Company', because i wasn't using full namespaces in my relationships, but now i'm getting this new error.

Error

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)

Upvotes: 6

Views: 4141

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

Use full namespace in the relationship code:

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

Upvotes: 8

Related Questions