【问题标题】:Laravel - Pluck multiple columnsLaravel - 采摘多列
【发布时间】:2020-04-20 14:05:16
【问题描述】:

我需要从表 corporate_objectives 中提取两列 namescore 并将其放入我的图表中。我有两种不同的行为,我似乎无法得到我想要的结果。

第一个代码

$getNameAndScore = CorporateObjective::pluck('name');

foreach($getNameAndScore as $key => $item) {
  $corporateObjective[] = [$item, '('.$key.'%)'];
}

结果:

    "xAxis": [
    [
      "PEOPLE DEVELOPMENT",
      "(0%)"
    ],
    [
      "OPTIMUM SYSTEMS AND PROCESSES",
      "(1%)"
    ],
    [
      "CUSTOMER MANAGEMENT",
      "(2%)"
    ],
    [
      "REVENUE GROWTH",
      "(3%)"
    ]
  ],

第二个代码

$getNameAndScore = CorporateObjective::pluck('name', 'score');

foreach($getNameAndScore as $key => $item) {
  $corporateObjective[] = [$item, '('.$key.'%)'];
}

结果:

  "xAxis": [
    [
      "REVENUE GROWTH",
      "(25%)"
    ]
  ],

我在第一个代码中得到了所有正确的 name 但不正确的 score。在我的第二个代码中,我得到了正确的namescore,但所有数据都没有被提取出来。我想用第二个代码中所有正确的score 来实现第一个代码。

编辑: 这就是我的数据库的样子

id | name                          | score
1    PEOPLE DEVELOPMENT              25
2    OPTIMUM SYSTEMS AND PROCESSES   25
3    CUSTOMER MANAGEMENT             25
4    REVENUE GROWTH                  25

除了pluck,还有其他方法吗?似乎pluck 合并/过滤了具有相同值的所有数据。

【问题讨论】:

  • laravel.com/docs/7.x/collections#method-pluck $getNameAndScore->all() 返回什么?
  • 第一个和第二个代码的输出相同。我想我明白了,所有其他四个都有相同的 25% score 所以所有分数都合并了。有没有办法不将它们合并到pluck 中?我将编辑我的问题。

标签: laravel


【解决方案1】:

这是您的代码的正确输出。这里没有问题

$getNameAndScore = CorporateObjective::pluck('name', 'score');

foreach($getNameAndScore as $key => $item) {
    $corporateObjective[] = [$item, '('.$key.'%)'];
}

如何工作pluck 这里是描述

如果存在重复键,则将最后一个匹配的元素插入到提取的集合中:

$collection = collect([
    ['brand' => 'Tesla',  'color' => 'red'],
    ['brand' => 'Pagani', 'color' => 'white'],
    ['brand' => 'Tesla',  'color' => 'black'],
    ['brand' => 'Pagani', 'color' => 'orange'],
]);

$plucked = $collection->pluck('color', 'brand');

$plucked->all();

// ['Tesla' => 'black', 'Pagani' => 'orange']

Details in here

【讨论】:

    【解决方案2】:

    所以我只是做了另一种方法,它可能会帮助其他人。如果有更正确或更清洁的方法,请随时纠正我的答案。

    $getNameAndScore = CorporateObjective::pluck('name');
    
    foreach($getNameAndScore as $item) {
      $key = CorporateObjective::where('name', $item)->value('score');
      $corporateObjective[] = [$item, '('.$key.'%)'];
    }
    return response()->json([
      'xAxis' => $corporateObjective,
    ]);
    

    结果

     "xAxis": [
        [
          "PEOPLE DEVELOPMENT",
          "(25%)"
        ],
        [
          "OPTIMUM SYSTEMS AND PROCESSES",
          "(1%)" // I changed the value in the database and it works
        ],
        [
          "CUSTOMER MANAGEMENT",
          "(22%)" // I changed the value in the database and it works
        ],
        [
          "REVENUE GROWTH",
          "(25%)"
        ]
      ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多