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

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

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的员工。

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

代码:

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

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

模型 Employee

  1. class Employee extends Model
  2. {
  3. protected $table = 'employees';
  4. protected $fillable = [
  5. 'name',
  6. 'surname',
  7. 'inactive',
  8. 'entered_into_service'
  9. ];
  10. public function employee_label() {
  11. return $this->hasOne('App\EmployeeLabel');
  12. }
  13. }

模型 EmployeeLabel

  1. class EmployeeLabel extends Model
  2. {
  3. protected $table = 'employee_label';
  4. protected $fillable = [
  5. 'employee_id',
  6. 'label_id'
  7. ];
  8. public function employee() {
  9. return $this->belongsTo('App\Employee');
  10. }
  11. public function label() {
  12. return $this->belongsTo('App\Label');
  13. }
  14. }
英文:

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

The table employee_label looks like this:

  1. +----+-------------+----------+
  2. | id | employee_id | label_id |
  3. +----+-------------+----------+
  4. | 1 | 123 | 123 |
  5. | 2 | 456 | 456 |
  6. | 3 | 768 | 768 |
  7. +----+-------------+----------+

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:

  1. $employees = Employee::with([&#39;employee_label&#39; =&gt; function($query) use ($request) {
  2. $query-&gt;where(&#39;label_id&#39;, &#39;=&#39;, $request-&gt;get(&#39;label&#39;));
  3. }])-&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 />

  1. class Employee extends Model
  2. {
  3. protected $table = &#39;employees&#39;;
  4. protected $fillable = [
  5. &#39;name&#39;,
  6. &#39;surname&#39;,
  7. &#39;inactive&#39;,
  8. &#39;entered_into_service&#39;
  9. ];
  10. public function employee_label() {
  11. return $this-&gt;hasOne(&#39;App\EmployeeLabel&#39;);
  12. }
  13. }

Model EmployeeLabel<br />

  1. class EmployeeLabel extends Model
  2. {
  3. protected $table = &#39;employee_label&#39;;
  4. protected $fillable = [
  5. &#39;employee_id&#39;,
  6. &#39;label_id&#39;
  7. ];
  8. public function employee() {
  9. return $this-&gt;belongsTo(&#39;App\Employee&#39;);
  10. }
  11. public function label() {
  12. return $this-&gt;belongsTo(&#39;App\Label&#39;);
  13. }
  14. }

答案1

得分: 0

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

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

所以最终的代码将是:

  1. $employees = Employee::whereHas('employee_label', function($query) use ($request) {
  2. $query->where('label_id', '=', $request->get('label'));
  3. })->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:

  1. $employees = Employee::whereHas(&#39;employee_label&#39;, function($query) use ($request) {
  2. $query-&gt;where(&#39;label_id&#39;, &#39;=&#39;, $request-&gt;get(&#39;label&#39;));
  3. })-&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:

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

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:

  1. $employees = Employee::all()->with('employeeLabel')
  2. ->orderBy('inactive', 'asc')
  3. ->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:

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

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:

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

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

答案3

得分: -1

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

而不是

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

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

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

I think you forgot to return your $query object

Instead of

  1. $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

  1. 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:

确定