【问题标题】:Fetching a column data of another table using id Laravel使用 id Laravel 获取另一个表的列数据
【发布时间】:2017-02-09 18:15:44
【问题描述】:

我有一个表,其中有一列 interviewer_id 等于用户表的 id。我根据控制器中的interviewer_id 对数据进行分组,并希望从 users 表中获取属于每个组的 id 的名称。这是我尝试过的

$int_payment = IntPayments::where('month',$month_year)->groupBy('interviewer_id')->get();
    foreach ($int_payment as $inter) {
        $inters = User::where('id',$inter->interviewer_id)->get();

    }
    return response()->json(['inters',$inters]);

这里我的回复只显示了它通过用户获取的一组数据。我想从用户表中获取 id 是组 ID 的行

这正是我想要的。当我返回 $int_payment 作为响应时,我得到了这个响应。

["inters",[{"id":2,"candidate_id":"13","interviewer_id":"1","profile_id":"","exp":"5-10","payment_status":"unpaid","month":"Feb 2017","created_at":"2017-02-08 04:56:40","updated_at":"2017-02-08 04:56:40"},{"id":4,"candidate_id":"13","interviewer_id":"2\r\n","profile_id":"","exp":"5-10","payment_status":"unpaid","month":"Feb 2017","created_at":"2017-02-08 04:56:40","updated_at":"2017-02-08 04:56:40"},{"id":1,"candidate_id":"13","interviewer_id":"4","profile_id":"","exp":"5-10","payment_status":"unpaid","month":"Feb 2017","created_at":"2017-02-08 04:56:40","updated_at":"2017-02-08 04:56:40"}]]

如果您看到响应,则有 id 为 2,4 和 1 的三个组。这些 id 是用户表中用户的 id。我想获取属于 ids 的用户名。

【问题讨论】:

    标签: laravel laravel-5.2


    【解决方案1】:

    尝试在数组中返回$inters

    foreach ($int_payment as $inter) {
            $inters[] = User::where('id',$inter->interviewer_id)->get();
        }
    return $inters;
    

    但是,在 User 和 IntPayment 模型之间建立关系会更容易处理这种情况。请参阅文档Eloquent relationship.

    【讨论】:

    • 但是您的回答让我从 USER 表中获取数据。但我只想要该表中的名称列,而 IntPayments 中的数据是我的首要任务。看到这个。你会更好地理解这个场景stackoverflow.com/questions/42129360/…
    • @NaveenKumar 学习和使用Eloquent relationship,而不是尝试那种方式。通过关系设置,只需一步一查询即可实现您想要的
    【解决方案2】:

    试试这个:

    IntPayments::select('interviewr_TABLE.*', 'users.name')->where('month',$month_year)->leftJoin('users', 'users.id', '=', 'interviewr_TABLE') ->groupBy('interviewer_id')->get()
    

    这里唯一的问题是我不确切知道您的 IntPayments 表名称是什么。

    【讨论】:

    • 我不使用表名,而是使用模型获取数据
    • 这是一个简单的leftJoin,在这种情况下没有任何问题,但是您可以将left join定义为模型关系。
    • 但是您的回答让我从 USER 表中获取数据。但我只想要该表中的名称列,而 IntPayments 中的数据是我的首要任务。看到这个。你会更好地理解这个场景stackoverflow.com/questions/42129360/…
    【解决方案3】:

    简单使用 Laravel 模型关系,让生活更轻松:

    让你User模特拥有:

    public function intpayments(){
        $this->hasMany(IntPayments::class, 'interviewer_id');
    }
    

    那么你的IntPayments 模型有:

    public function user(){
        $this->belongsTo(User::class, 'interviewer_id');
    }
    

    因此您可以轻松地检索到您想要的相同结果:

    $inters = IntPayments::where('month',$month_year)->load('user');
    

    然后你可以遍历这个结果来获取你需要说的字段:

    $result = [];
        foreach($inters as $inter)
        {
           $result[] = $inter->user->name;
        }
    return $result;
    

    更新:

    但我只想要该表中的名称列,而 IntPayments 中的数据是我的首要任务

    好的,您仍然可以使用查询返回的相同结果:

    $result = [];
        foreach($inters as $k => $inter)
        {
           $result[$k]['name'] = $inter->user->name; //this is the user's name
           $result[$k]['id'] = $inter->id; //this is intPayments's id
           $result[$k]['interviewer_id'] = $inter->interviewer_id; //this is intPayments interviewer's 'id
           ......
        }
    

    也许你不知道,$inters 变量的结果会首先给你,IntPayments 的每条记录,然后是User 的每条记录。

    这意味着inters自然会像(如果我以json形式发送响应):

    [
      {
        "id": 1,
        "candidate_id": "1",
        "interviewer_id": "3"
        "user": {
          "id": 1,
          "name": "second last",
          "email": "second@somemail.com",
        }
      },
      {
        "id": 2,
        "candidate_id": "1",
        "interviewer_id": "5",
        "user": {
        "id": 1,
          "name": "first last",
          "email": "first@somemail.com",
        }
      },
      .....
    ]
    

    这意味着您可以根据需要使用此响应并访问所需的字段。

    希望这会有所帮助:)

    【讨论】:

    • 但是你的回答让我得到了 USER 表中的数据。但我只想要该表中的名称列,而 IntPayments 中的数据是我的首要任务。看到这个。你会更好地理解这个场景stackoverflow.com/questions/42129360/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2021-02-06
    • 2021-10-19
    • 2020-09-08
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    相关资源
    最近更新 更多