英文:
Getting user ID instead of volunteer ID while joining tables in Laravel GraphQL search handler
问题
在搜索处理程序中连接两个表时,如果两个表中有相同的列名,我无法访问左表的值。例如,如果有两个表"user"和"volunteers",它们都有"id"列,当我编写以下搜索处理程序时:
$builder->join('users', 'volunteers.user_id', '=', 'users.id')
->join('policies', 'volunteers.policy_id', '=', 'policies.id')
->where(function($q) use ($whereConditions){
$q->where('users.first_name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('users.last_name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('policies.name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('volunteers.experiences', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('volunteers.medical_facility', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
});
当我查询时,它将返回用户的ID作为志愿者的ID。我想要志愿者的ID,但我总是得到用户的ID。希望问题清楚明了。
英文:
When i join two tables in search handler there is a same column in both tables, i cannot access the value of left table
for example if there are two tables user and volunteers they both have id column when I write a search handler like this
$builder->join('users', 'volunteers.user_id', "=", "users.id")
->join('policies','volunteers.policy_id',"=","policies.id")
->where(function($q) use ($whereConditions){
$q->where('users.first_name','like','%'.$whereConditions['OR'][0]['value'].'%');
$q->orWhere('users.last_name','like','%'.$whereConditions['OR'][0]['value'].'%');
$q->orWhere('policies.name','like','%'.$whereConditions['OR'][0]['value'].'%');
$q->orWhere('volunteers.experiences','like','%'.$whereConditions['OR'][0]['value'].'%');
$q->orWhere('volunteers.medical_facility','like','%'.$whereConditions['OR'][0]['value'].'%');
});
and when i query, it will return user id as volunteer id
I want the volunteer id but I always get the user id. I hope the question is clear
答案1
得分: 0
为了解决这个问题,请确保在你的连接语句中使用了正确的列名。如果 volunteers 表中有一个名为 volunteer_id 的列,表示志愿者的ID,你应该像这样更新你的代码:
$builder->join('users', 'volunteers.volunteer_id', '=', 'users.id')
->join('policies', 'volunteers.policy_id', '=', 'policies.id')
->where(function ($q) use ($whereConditions) {
$q->where('users.first_name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('users.last_name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('policies.name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('volunteers.experiences', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('volunteers.medical_facility', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
});
在上面的代码中,我假设 volunteer_id 列存在于 volunteers 表中,并表示志愿者的ID。如果在你的数据库架构中与 volunteer_id 不同,请确保替换为正确的列名。
通过更新连接条件以使用正确的列名,你应该能够在查询结果中检索到志愿者的ID,而不是用户ID。
英文:
To fix this issue, you need to make sure that you are using the correct column names in your join statement. If the volunteers table has a column named volunteer_id which represents the ID of a volunteer, you should update your code like this:
$builder->join('users', 'volunteers.volunteer_id', '=', 'users.id')
->join('policies', 'volunteers.policy_id', '=', 'policies.id')
->where(function ($q) use ($whereConditions) {
$q->where('users.first_name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('users.last_name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('policies.name', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('volunteers.experiences', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
$q->orWhere('volunteers.medical_facility', 'like', '%' . $whereConditions['OR'][0]['value'] . '%');
});
In the above code, I assumed that the column volunteer_id exists in the volunteers table and represents the ID of a volunteer. Make sure to replace volunteer_id with the correct column name if it differs in your database schema.
By updating the join condition to use the correct column name, you should be able to retrieve the volunteer ID instead of the user ID in your query results.
答案2
得分: 0
我实现了你的问题,出现了问题,但我通过使用$builder->select('你的列')
解决了它。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论