英文:
Laravel 5.8 - Eloquent not listening to where on relation
问题
我试图在我的关系中执行一个名为employee_label
的where
查询。
表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(['employee_label' => function($query) use ($request) {
$query->where('label_id', '=', $request->get('label'));
}])->where($searchQuery)->orWhere($orQuery)->orderBy('inactive', 'asc')->paginate(20);
What I've tried:<br />
I've tried to change the $query->where('label_id')
to $query->where('employee_label.label_id')
with no change in the result.
Model Employee
<br />
class Employee extends Model
{
protected $table = 'employees';
protected $fillable = [
'name',
'surname',
'inactive',
'entered_into_service'
];
public function employee_label() {
return $this->hasOne('App\EmployeeLabel');
}
}
Model EmployeeLabel
<br />
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');
}
}
答案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('employee_label', function($query) use ($request) {
$query->where('label_id', '=', $request->get('label'));
})->where($searchQuery)->orWhere($orQuery)->orderBy('inactive', 'asc')->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->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);
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->where('label_id', '=', $request->get('label'));
Try returning your $query from a employee_label callback method
return $query->where('label_id', '=', $request->get('label'));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论