StephanHoeksema's avatar

How to get data in eloquent where we have companies , users that belong to the companies and the users have profiles?

How to get data in eloquent where we have companies , users that belong to the companies and the users have profiles?

Model Company:

public function users() { return $this->belongsToMany(User::class); }

Model Users:

public function role() { return $this->belongsTo(Role::class); }

public function profile()
{
    return $this->hasOne(Profile::class, 'user_id');
}

public function companies()
{
    return $this->belongsToMany(Company::class);
}

Model Role:

public function user() { return $this->hasMany(User::class); }

From a User I can get it's profile an the company is belongs to. But not the inverse. I want to get a company it's users and the profile of those users. Can anyone help?

0 likes
4 replies
LaryAI's avatar
Level 58

To retrieve a company with its users and the profiles of those users, you can use Eloquent's eager loading feature. This allows you to load all the related data in a single query, reducing the number of queries to the database.

Here's how you can achieve this:

// Retrieve a specific company by its id with users and their profiles
$companyId = 1; // Replace with the actual company id you want to query
$company = Company::with('users.profile')->find($companyId);

// If you want to retrieve all companies with their users and profiles
$companies = Company::with('users.profile')->get();

This will give you a collection of companies, each with their related users and the users' profiles loaded. You can then iterate over the collection and access the data as needed:

foreach ($companies as $company) {
    echo "Company: " . $company->name . "\n";
    foreach ($company->users as $user) {
        echo "User: " . $user->name . "\n";
        if ($user->profile) {
            echo "Profile: " . $user->profile->bio . "\n"; // Assuming 'bio' is a field in the profile
        }
    }
}

Make sure that your User and Profile models are set up correctly to allow for this relationship:

// User model
class User extends Authenticatable
{
    // ...

    public function profile()
    {
        return $this->hasOne(Profile::class, 'user_id');
    }

    public function companies()
    {
        return $this->belongsToMany(Company::class);
    }

    // ...
}

// Profile model
class Profile extends Model
{
    // ...

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

    // ...
}

Remember to replace 'user_id' with the actual foreign key if it's different in your database schema. Also, replace 'bio' with the actual field you want to access from the Profile model.

1 like
Snapey's avatar

your relationships seem wrong

user belongs to a single company and a company has many users ?

Or, you have a company_users pivot table and

user can belong to many companies and a company has many users?

1 like
StephanHoeksema's avatar

Indeed I have a pivot table did not post that. user can belong to many companie and a company had many users. The eager loading part works perfect!

Please or to participate in this conversation.