我的问题有三个表:“comments”、“users”和“company_contacts”
“评论”表有两列:“company_contact (INT)”
和 “company_contact_kind (String)”
comments.company_contact
根据 comments.company_contact_kind = ' 分配给
或如果 users.id
或 company_contacts.id
contact'comments.company_contact_kind = 'user'
这是我的查询:
$data['comments'] = Comment::join('users', 'comments.creator_id', '=', 'users.id')
->join('users AS user_contacts', 'comments.company_contact', '=', 'user_contacts.id')
->join('company_contacts', 'company_contacts.id', '=', 'comments.company_contact')
->where('comments.commentable_type', $request->type)
->where('comments.commentable_id', $request->company_id)
->where(function($query) use ($request){
$query->orWhere('body', 'LIKE', '%' . $request->q . '%')
->orWhere('users.first_name', 'LIKE', '%' . $request->q . '%')
->orWhere('users.last_name', 'LIKE', '%' . $request->q . '%')
->orWhere('user_contacts.first_name', 'LIKE', '%' . $request->q . '%')
->orWhere('user_contacts.last_name', 'LIKE', '%' . $request->q . '%')
->orWhere('company_contacts.first_name', 'LIKE', '%' . $request->q . '%')
->orWhere('company_contacts.last_name', 'LIKE', '%' . $request->q . '%')
->orWhere('comments.contact_type', 'LIKE', '%' . $request->q . '%')
->orWhere('tags', 'LIKE', '%' . $request->q . '%');
})
->orderBy('comments.created_at', 'DESC')
->select('comments.*')
->get();
我的问题:
搜索特定评论联系人时,因为我在查询中引用了用户和公司联系人,并且两者都等于 comments.company_contact
,如果我搜索 company_contacts.id 的名字或姓氏
,它将返回以 users.id
作为 comments.company_contact
的结果,因为 comments.company_contact
引用了 users 和 company_contacts。
有没有办法在查询中设置条件以获得更直观的结果?
已解决:
感谢@cmerriman提出的答案,我能够调整答案并通过以下查询解决它:
$data['comments'] = Comment::join('users', 'comments.creator_id', '=', 'users.id')
->leftJoin('users AS user_contacts', function ($join) {
$join->where('comments.company_contact_kind', '=', 'user')
->on('comments.company_contact', '=','user_contacts.id');
})
->leftJoin('company_contacts', function ($join) {
$join->where('comments.company_contact_kind', '=', 'contact')
->on('comments.company_contact', '=','company_contacts.id');
})
->where('comments.commentable_type', $request->type)
->where('comments.commentable_id', $request->company_id)
->where(function($query) use ($request){
$query->orWhere('body', 'LIKE', '%' . $request->q . '%')
->orWhere('users.first_name', 'LIKE', '%' . $request->q . '%')
->orWhere('users.last_name', 'LIKE', '%' . $request->q . '%')
->orWhere('user_contacts.first_name', 'LIKE', '%' . $request->q . '%')
->orWhere('user_contacts.last_name', 'LIKE', '%' . $request->q . '%')
->orWhere('company_contacts.first_name', 'LIKE', '%' . $request->q . '%')
->orWhere('company_contacts.last_name', 'LIKE', '%' . $request->q . '%')
->orWhere('comments.contact_type', 'LIKE', '%' . $request->q . '%')
->orWhere('tags', 'LIKE', '%' . $request->q . '%');
})
->orderBy('comments.created_at', 'DESC')
->select('comments.*')
->get();
最佳答案
我从未使用过 Laravel,但如果我正确阅读文档,请尝试更改
->join('users AS user_contacts', 'comments.company_contact', '=', 'user_contacts.id')
->join('company_contacts', 'company_contacts.id', '=', 'comments.company_contact')
至
->join('users AS user_contacts', function ($join) {
$join->on('comments.company_contact', '=','user_contacts.id')
->andOn('comments.company_contact_kind, '=', 'user'); } )
->join('companyusers AS user_contacts', function ($join) {
$join->on('comments.company_contact', '=','company_contacts.id')
->andOn('comments.company_contact_kind, '=', 'contact'); } )
关于php - "LIKE"对表进行查询,根据关系类型将外键分配给多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44570986/