代码之家  ›  专栏  ›  技术社区  ›  George

如何为拉维收藏估价?

  •  2
  • George  · 技术社区  · 7 年前

    如何给拉维收藏的第一个元素赋值?差不多吧 $collection->put('foo', 1)

    Collection {#376
      #items: array:1 [
        0 => array:9 [
          "id" => 83
          "status" => "offline"
          "created_date" => "Oct 31, 2018"
          // add foo => 1 here
        ]
      ]
    }
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   kmuenkel    7 年前

    我怀疑有一个更干净的方法可以做到这一点,但这是我目前能想到的最好的方法。你也可以用 map transform

    $collection = collect([
        [
            'id' => 83,
            'status' => 'offline',
            'created_date' => 'Oct 31, 2018'
        ]
    ]);
    
    $firstKey = $collection->keys()->first();  //This avoids the unreliable assumption that your index is necessarily numeric.
    $firstElement = $collection->first();
    $modifiedElement = array_merge($firstElement, ['foo1' => 1]);
    $collection->put($firstKey, $modifiedElement);
    
        2
  •  2
  •   Shailendra Gupta    7 年前

    用这个

    $data = Model::all();
    $data[0]->foo = 'your data here';
    
    推荐文章