1

Following are my relations in laravel , I am unable to access company using User’s Object, but i am getting null when i try to access it, please see my code below to get the picture

following are my models

<?php

namespace AppModels;

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
use ZizacoEntrustTraitsEntrustUserTrait;
use Session;
use IlluminateSupportFacadesDB;

class User extends Authenticatable
{
    use Notifiable;
    use EntrustUserTrait;

    protected $table = 'tbl_users';
    protected $primaryKey = 'id';
    protected $guarded = ['id'];
    const API = 'api';
    const WEB = 'web';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'last_login', 'Address', 'Age', 'DateOfBirth', 'created_by', 'deleted_by'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'is_admin' => 'boolean',
    ];

    public function isAdmin()
    {
        return $this->is_admin;
    }

    static function GetUserNamebyID($id)
    {
        $name = User::select("name")->where(["id" => $id])->pluck('name');
        if (isset($name[0])) {
            return $name[0];
        } else {
            return '';
        }
    }



    public function loginlogout()
    {
        $this->hasMany("AppModelsLoginLogoutLogs", 'userID');
    }

    public function company()
    {
        $this->hasMany("AppModelsCompany", "company_id");
    }
}

And following is my companies Model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModel;
use DB;
use AppModelsCarePlanData;
use Session;

class Company extends Model
{
    protected $table = 'companies';
    protected $primaryKey = 'id';
    protected $guarded = ['id'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'phone_no', 'address', 'password', 'description', 'city', 'company_logo', 'country', 'email'
    ];

    static public function fetchAllActiveCompanies()
    {

        return DB::table("companies")->where(['is_active' => 1])->pluck('name', 'id');
    }

// change company to hasmany

    public function users()
    {
        return $this->hasmany('AppModelsUser');
    }

}

and this is how i am trying to access the Company , but i am getting null.

public function fetchCompany(){
 $User = User::find($user->id);
        dd($User->company());
}