many to many working oneway only

Posted 2 years ago by arctushar

My User.php code is

    public function projects()
    {
        return $this->belongsToMany('App\Project');
    }

    public function companies()
    {
        return $this->belongsToMany('App\Company');
    }   

My projects table has below columns

id
project_name
project_address
project_basements
project_floors
project_land_area
project_description
created_at
updated_a

and My project_user table has

user_id
project_id
user_weight
created_at
updated_at

When I call

$user=Auth::user()->projects;

its working well but My companies table has

id
company_name
company_address
company_phone
created_at
updated_at

and My company_user has

user_id
company_id
created_at
updated_a

When I call

$user=Auth::user()->companies;

then not working. All tables has data. company_user table has user_id=1,company_id=1 what is the problem ??

return Company::find(2)->users; 

working good but

return User::find(1)->companies;

giving error, what is the problem ?

Please sign in or create an account to participate in this conversation.