User.php
class User extends Authenticatable
{
public function company() {
return $this->belongsTo('App\Company');
}
}
Company.php
class Company extends Model
{
public function users() {
return $this->hasMany('App\User');
}
}
Table users
id name company_id
Company table
id name
I am trying to get the name of the company attached to the user.
$user = User::findOrFail(Auth::user()->id);
$companyName = $user->company()->first()->name;
I got this error message Trying to get property of non-object
I do not understand what I am missing ... Thanks in advance
source
share