Laravel 5.8 – Eloquent在关联上未监听到”where”条件。

huangapple go评论75阅读模式
英文:

Laravel 5.8 - Eloquent not listening to where on relation

问题

我试图在我的关系中执行一个名为employee_labelwhere查询。

employee_label看起来像这样:

+----+-------------+----------+
| id | employee_id | label_id |
+----+-------------+----------+
| 1 | 123 | 123 |
| 2 | 456 | 456 |
| 3 | 768 | 768 |
+----+-------------+----------+

其他的where()orWhere()默认传递一个空的array(),但可能包含数据,例如:['inactive' => 0]

预期结果:
当我在关系中提供标签123作为where()条件时,我希望只收到具有标签123的员工。

实际结果:
返回所有员工,数据未被过滤。

代码:

$employees = Employee::with(['employee_label' => function($query) use ($request) {
    $query->where('label_id', '=', $request->get('label'));
}])->where($searchQuery)->orWhere($orQuery)->orderBy('inactive', 'asc')->paginate(20);

我尝试过的事情:
我尝试将$query->where('label_id')更改为$query->where('employee_label.label_id'),但结果没有改变。

模型 Employee

class Employee extends Model
{
    protected $table = 'employees';
    protected $fillable = [
        'name',
        'surname',
        'inactive',
        'entered_into_service'
    ];

    public function employee_label() {
        return $this->hasOne('App\EmployeeLabel');
    }
}

模型 EmployeeLabel

class EmployeeLabel extends Model
{

    protected $table = 'employee_label';
    protected $fillable = [
        'employee_id',
        'label_id'
    ];

    public function employee() {
        return $this->belongsTo('App\Employee');
    }

    public function label() {
        return $this->belongsTo('App\Label');
    }

}
英文:

I'm trying to do a where query on my relation called employee_label.

The table employee_label looks like this:

+----+-------------+----------+
| id | employee_id | label_id |
+----+-------------+----------+
|  1 |         123 |      123 |
|  2 |         456 |      456 |
|  3 |         768 |      768 |
+----+-------------+----------+

The other where() and orWhere() get passed an empty array() by default, but could contain data as for example: ['inactive' => 0].

Expected result: <br/>
I expect to only receive the employees with for example label 123 when I provide label 123 as where() for the relation.

Actual result: <br/>
All employees are returned, the data is not filtered.

Code:

$employees = Employee::with([&#39;employee_label&#39; =&gt; function($query) use ($request) {
    $query-&gt;where(&#39;label_id&#39;, &#39;=&#39;, $request-&gt;get(&#39;label&#39;));
}])-&gt;where($searchQuery)-&gt;orWhere($orQuery)-&gt;orderBy(&#39;inactive&#39;, &#39;asc&#39;)-&gt;paginate(20);

What I've tried:<br />
I've tried to change the $query-&gt;where(&#39;label_id&#39;) to $query-&gt;where(&#39;employee_label.label_id&#39;) with no change in the result.

Model Employee<br />

class Employee extends Model
{
    protected $table = &#39;employees&#39;;
    protected $fillable = [
        &#39;name&#39;,
        &#39;surname&#39;,
        &#39;inactive&#39;,
        &#39;entered_into_service&#39;
    ];

    public function employee_label() {
        return $this-&gt;hasOne(&#39;App\EmployeeLabel&#39;);
    }

}

Model EmployeeLabel<br />

class EmployeeLabel extends Model
{

    protected $table = &#39;employee_label&#39;;
    protected $fillable = [
        &#39;employee_id&#39;,
        &#39;label_id&#39;
    ];

    public function employee() {
        return $this-&gt;belongsTo(&#39;App\Employee&#39;);
    }

    public function label() {
        return $this-&gt;belongsTo(&#39;App\Label&#39;);
    }

}

答案1

得分: 0

经过长时间的搜索,我终于找到了这个奇怪问题的答案。

显然,不要使用 Employee::with(),而是要使用 Employee::whereHas()

所以最终的代码将是:

$employees = Employee::whereHas('employee_label', function($query) use ($request) {
    $query->where('label_id', '=', $request->get('label'));
})->where($searchQuery)->orWhere($orQuery)->orderBy('inactive', 'asc')->paginate(20);

有关更多信息,请查看文档

感谢所有帮助过我的人!

英文:

After a long search I've finally found the answer to this weird problem.

Apparently instead of using Employee::with() you have to use Employee::whereHas().

So the final code will be:

$employees = Employee::whereHas(&#39;employee_label&#39;, function($query) use ($request) {
    $query-&gt;where(&#39;label_id&#39;, &#39;=&#39;, $request-&gt;get(&#39;label&#39;));
})-&gt;where($searchQuery)-&gt;orWhere($orQuery)-&gt;orderBy(&#39;inactive&#39;, &#39;asc&#39;)-&gt;paginate(20);

See the documentation for more information.

Thanks all who helped!

答案2

得分: 0

Try updating the employee model to:

public function employeeLabel() {
    return $this->hasOne('App\EmployeeLabel', 'employee_id', 'id');
}

That binds the relationship automatically so you do not need to in the eloquent call.

Then the method to get all employees and associated labels and return paginated with:

$employees = Employee::all()->with('employeeLabel')
                            ->orderBy('inactive', 'asc')
                            ->paginate(20);

You can find more information here: https://laravel.com/docs/6.x/eloquent-relationships#one-to-one

英文:

Try updating the employee model to:

    public function employeeLabel() {
        return $this-&gt;hasOne(&#39;App\EmployeeLabel&#39;, &#39;employee_id&#39;, &#39;id&#39;);
    }

That binds the relationship automatically so you do not need to in the eloquent call.

Then the method to get all employees and associated labels and return paginated with:

$employees = Employee::all()-&gt;with(&#39;employeeLabel&#39;)
                            -&gt;orderBy(&#39;inactive&#39;, &#39;asc&#39;)
                            -&gt;paginate(20);

https://laravel.com/docs/6.x/eloquent-relationships#one-to-one

答案3

得分: -1

我认为你忘记了返回你的 $query 对象

而不是

$query->where('label_id', '=', $request->get('label'));

尝试从 employee_label 回调方法中返回你的 $query

return $query->where('label_id', '=', $request->get('label'));
英文:

I think you forgot to return your $query object

Instead of

$query-&gt;where(&#39;label_id&#39;, &#39;=&#39;, $request-&gt;get(&#39;label&#39;));

Try returning your $query from a employee_label callback method

return $query-&gt;where(&#39;label_id&#39;, &#39;=&#39;, $request-&gt;get(&#39;label&#39;));

huangapple
  • 本文由 发表于 2020年1月6日 15:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定