fill($data); $user->save(); if(isset($data['company_user'])) { $company = auth()->user()->company(); $account_id = $company->account->id; $cu = CompanyUser::whereUserId($user->id)->whereCompanyId($company->id)->first(); /*No company user exists - attach the user*/ if(!$cu){ $data['company_user']['account_id'] = $account_id; $user->companies()->attach($company->id, $data['company_user']); } else { $cu->fill($data['company_user']); $cu->save(); } } return $user; } }