php - "LIKE"对表进行查询,根据关系类型将外键分配给多个表

标签 php mysql laravel foreign-key-relationship laravel-query-builder

我的问题有三个表:“comments”、“users”和“company_contacts”

“评论”表有两列:“company_contact (INT)”“company_contact_kind (String)”

comments.company_contact 根据 comments.company_contact_kind = ' 分配给 users.idcompany_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/

相关文章:

php - 如何使用 php 更新 mysql 的当前日期?

javascript - php 在页面加载时检查 javascript

mysql - 多态关联替代

laravel - 捕捉 Eloquent 错误

php - 如何强制 Composer 重新安装库?

php - 如何访问 Laravel 中某些特定 Controller 的数据?

php - 显示来自 MySQL 的特定 PHP 数组

java - Mysql/Glassfish 无效资源 jdbc

php - SQL 查询未被执行

php - 插入错误(PHP)