Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 425
S4boteur Avatar asked May 12 '17 17:05

S4boteur


1 Answers

Use full namespace in the relationship code:

public function company()
{
    return $this->belongsTo('App\Company', 'user_id');
}
like image 147
Alexey Mezenin Avatar answered Oct 18 '22 21:10

Alexey Mezenin