mercredi 21 septembre 2016

Get Collection With HasOne Relationship

In my User model in Laravel 5.2 I have a relationship setup with their status to the company.

public function companyStatus()
{
    return $this->hasOne('CompanyUser')->select('status');
}

The CompanyUser table has a company_id, user_id, and status field

Then in my controller I do the following:

    $company   = Company::find($company_id);
    $users     = CompanyUser::where('company_id', $company_id)->pluck('user_id')->toArray();
    $user_data = User::with('companyStatus')->find($users);

but when I dump the user_data array it has all of the users related to the company, but just shows null for their status relationship

    {
       "id":2,
       "name":"Moderator",
       "email":"mod@company.com",
       "created_at":"2016-09-08 15:26:20",
       "updated_at":"2016-09-08 15:26:25",
       "company_status":null
     }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire