0

In my Laravel 8 application, I have the following models.

Company

protected $fillable = [
    'id',
    'name',
    'website',
    'company_logo',
    'registration_number',
    'date_established',
    'address',
];

Employee

protected $fillable = [
    'id',
    'company_id',
    'user_id',
    'first_name',
    'last_name',
    'other_name',
    'gender',
];

public function company()
{
    return $this->belongsTo(Company::class,'company_id','id');
}

public function user()
{
    return $this->belongsTo(User::class,'user_id','id');
}

I started this but got stuck on the way. I want to select company details based on the logged user. The company table should be the main table:

public function getMyCompany()
{
    try {
        $userId = Auth::user()->id;
        $employeeId = Employee::where('user_id', $userId)->first();
        $company = Company::...;

        return $this->success('Company successfully Retrieved.', [
            'company' => $company
        ]);
    } catch (\Exception $e) {
        Log::error($e);

        return $this->error($e->getMessage(), $e->getCode());
    }
}

How do I achieve this (select all details of the company) using:

$company = Company::...;

Making the main model

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
user11352561
  • 2,277
  • 12
  • 51
  • 102
  • so you want the details of the company from the Auth::user() ? – Elias Jul 20 '21 at 14:23
  • The question needs more clarity. From the model, it looks like the Employee belongsTo 1 company, but you are trying to retrieve companies? Are you trying to retrieve the company the employee belongsTo or are you trying to retrieve all companies? – Abishek Jul 20 '21 at 16:46
  • @Abishek - I'm retrieving just one company. Employee belongs to company. I need to get only one company for the Employee. Company should be the main model. See the illustration in the models above – user11352561 Jul 20 '21 at 17:30
  • @user11352561, please make sure that the variable names you use are singular in that case, I have edited the question to signify that, but keep in mind for your reference, else this creates unnecessary confusion for people trying to help you. It should be `$company` and not `$companies` if you are trying to retrieve the company for the employee. Its a 1 to 1 relation – Abishek Jul 20 '21 at 17:36

2 Answers2

2

I'm not sure if you're expecting to get multiple companies from the user, or just a single one. The reason I'm not sure is that you have defined a 1-1 relationship between a company and an employee, yet it looks like you want getMyCompany() to return multiple companies.

If the idea is to retrieve only the one company that the employee works at, you can use the employee's "belongsTo"-relationship as following:

$company = $employee->company;

Since you have already retrieved the employee related to the authenticated user, and the employee model has a "company"-relationship.

If you want to do it in one go, you can chain the queries:

$company = Employee::where('user_id', Auth::user()->id)
                     ->first()
                     ->company;
Megadöden
  • 81
  • 3
1

Use Eloquent Eager loading for this as the Employee model has a belongsTo relationship for company

public function getMyCompany()
{
    try {
        $userId = Auth::user()->id;
        $employee = Employee::with('company')->where('user_id',$userId)->first();
        $company = $employee->company
        return $this->success('Company successfully Retrieved.', [
            'company'         => $company
        ]);
    } catch(\Exception $e) {
        Log::error($e);
        return $this->error($e->getMessage(), $e->getCode());
    }
}

Refer: https://laravel.com/docs/8.x/eloquent-relationships#eager-loading for how Eager Loading works in Laravel

Abishek
  • 11,191
  • 19
  • 72
  • 111