我正从我的数据库中检索员工数据。
该表看起来像这样(我遗漏了大部分不相关的列)
+------------------+-------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| home_address_id | int(11) | NO | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| deleted_at | datetime | YES | | NULL | |
+------------------+-------------------------------+------+-----+---------+----------------+
我的疑问:
($data
是用户对POST提出的要求)
Employee::where('deleted_at', '=', null)
->join('addresses', 'employees.home_address_id', '=', 'addresses.id')
->leftJoin('employee_has_types', 'employees.id', '=', 'employee_has_types.employee_id')
->where('first_name', 'LIKE', $data->first_name)
->where('last_name', 'LIKE', $data->last_name)
->where('type_id', 'LIKE', $typeid->type_id)
->where('street', 'LIKE', $data->street_name)
->where('house_number', 'LIKE', $data->house_number)
->where('zip_code', 'LIKE', $data->zip_code)
->where('city', 'LIKE', $data->city)
->where('country', 'LIKE', $data->country)
->where('birth_date', 'LIKE', $data->birth_date)
->where('gender', 'LIKE', $data->gender)
->where('github', 'LIKE', $data->github)
->where('nationality', 'LIKE', $data->nationality)
->where('email', 'LIKE', $data->email)
->where('key_skills', 'LIKE', $data->skills)->get();
当我var_dump()
结果时,它会显示两个预期结果。
但我的Id有一个问题。它显示相同的Id两次。
array (size=27)
'id' => int 1
//Rest of my data
和
array (size=27)
'id' => int 1
//Rest of the other data.
我做错了什么?数据在我的数据库中没有重复的ID。
答案 0 :(得分:0)
You need to add select
and groupBy
statment:
Employee::select('employees.*')where('deleted_at', '=', null)
->join('addresses', 'employees.home_address_id', '=', 'addresses.id')
->leftJoin('employee_has_types', 'employees.id', '=', 'employee_has_types.employee_id')
->where('first_name', 'LIKE', $data->first_name)
->where('last_name', 'LIKE', $data->last_name)
->where('type_id', 'LIKE', $typeid->type_id)
->where('street', 'LIKE', $data->street_name)
->where('house_number', 'LIKE', $data->house_number)
->where('zip_code', 'LIKE', $data->zip_code)
->where('city', 'LIKE', $data->city)
->where('country', 'LIKE', $data->country)
->where('birth_date', 'LIKE', $data->birth_date)
->where('gender', 'LIKE', $data->gender)
->where('github', 'LIKE', $data->github)
->where('nationality', 'LIKE', $data->nationality)
->where('email', 'LIKE', $data->email)
->where('key_skills', 'LIKE', $data->skills)
->groupBy('employees.id')
->get();
becaue you're using join queries so the id can be override for the other table :)