Is there a way to have table name automatically added to Eloquent query methods? : <p>I'm developing an app on Laravel 5.5 and I'm facing an issue with a specific query scope. I have the following table structure (some fields omitted):</p>

<pre><code>orders
---------
id
parent_id
status
</code></pre>

<p>The <code>parent_id</code> column references the <code>id</code> from the same table. I have this query scope to filter records that don't have any children:</p>

<pre><code>public function scopeNoChildren(Builder $query): Builder
{
    return $query-&gt;select('orders.*')
        -&gt;leftJoin('orders AS children', function ($join) {
            $join-&gt;on('orders.id', '=', 'children.parent_id')
                -&gt;where('children.status', self::STATUS_COMPLETED);
        })
        -&gt;where('children.id', null);
}
</code></pre>

<p>This scope works fine when used alone. However, if I try to combine it with any another condition, it throws an SQL exception:</p>

<pre><code>Order::where('status', Order::STATUS_COMPLETED)
    -&gt;noChildren()
    -&gt;get();
</code></pre>

<p>Leads to this:</p>

<blockquote>
  <p>SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous</p>
</blockquote>

<p>I found two ways to avoid that error:</p>

<h2>Solution #1: Prefix all other conditions with the table name</h2>

<p>Doing something like this works:</p>

<pre><code>Order::where('orders.status', Order::STATUS_COMPLETED)
    -&gt;noChildren()
    -&gt;get();
</code></pre>

<p>But I don't think this is a good approach since it's not clear the table name is required in case other dev or even myself try to use that scope again in the future. They'll probably end up figuring that out, but it doesn't seem a good practice.</p>

<h2>Solution #2: Use a subquery</h2>

<p>I can keep the ambiguous columns apart in a subquery. Still, in this case and as the table grows, the performance will degrade.</p>

<p>This is the strategy I'm using, though. Because it doesn't require any change to other scopes and conditions. At least not in the way I'm applying it right now.</p>

<pre><code>public function scopeNoChildren(Builder $query): Builder
{
    $subQueryChildren = self::select('id', 'parent_id')
        -&gt;completed();
    $sqlChildren = DB::raw(sprintf(
        '(%s) AS children',
        $subQueryChildren-&gt;toSql()
    ));

    return $query-&gt;select('orders.*')
        -&gt;leftJoin($sqlChildren, function ($join) use ($subQueryChildren) {
            $join-&gt;on('orders.id', '=', 'children.parent_id')
                -&gt;addBinding($subQueryChildren-&gt;getBindings());
         })-&gt;where('children.id', null);
}
</code></pre>

<h2>The perfect solution</h2>

<p>I think that having the ability to use queries without prefixing with table name without relying on subqueries would be the perfect solution.</p>

<p>That's why I'm asking: <strong>Is there a way to have table name automatically added to Eloquent query methods?</strong></p>
