donglian6625 2016-10-19 12:50
浏览 40

Laravel查询构建器检索错误的ID

I'm retrieving Employee data from my database.

The table looks like this (I left out most of the columns which are not relevant)

+------------------+-------------------------------+------+-----+---------+----------------+
| 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    |                |
+------------------+-------------------------------+------+-----+---------+----------------+

My query:
($data is the requirements the user has given over 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();

And when I var_dump() the result, it shows two results, which were expected.

But there is one problem with my Id. it shows the same Id twice.

array (size=27)
  'id' => int 1
//Rest of my data

and

    array (size=27)
  'id' => int 1
//Rest of the other data.

What have I done wrong? the data doesn't have duplicate Id's in my database.

  • 写回答

1条回答 默认 最新

  • douxiexie3574 2016-10-19 13:38
    关注

    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 :)

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探