在Laravel GraphQL搜索处理程序中,加入表时获取用户ID而不是志愿者ID。

huangapple go评论63阅读模式
英文:

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('你的列')解决了它。

在Laravel GraphQL搜索处理程序中,加入表时获取用户ID而不是志愿者ID。

在Laravel GraphQL搜索处理程序中,加入表时获取用户ID而不是志愿者ID。

英文:

I implemented your problem and the problem happened to me but I solved it by using $builder->select('your columns').
在Laravel GraphQL搜索处理程序中,加入表时获取用户ID而不是志愿者ID。

在Laravel GraphQL搜索处理程序中,加入表时获取用户ID而不是志愿者ID。

huangapple
  • 本文由 发表于 2023年6月1日 19:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定