Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

inkarnation's avatar

Relation not joined on Query Builder

Hi, I'm trying to get a query builder that is already joining a relation to eager load them (Unfortunately I need a "Query Builder" to be later used and passed to another package).

I'd suspect the following code to already join the two tables, but it doesn't:

> Employee::query()->with('company')->toSql();
= "select * from `employees`"

The parent model does have the relation defined:

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

Am I misusing the query builder functionality or do I miss something? I'd expect something like:

> Employee::query()->with('company')->toSql();
= "select * from `employees` left join `companies` on `employees`.`company_id` = `companies`.`id`"

Thanks a lot

0 likes
2 replies
Sinnbeck's avatar

With() does not use join. It's a separate query

Please or to participate in this conversation.