在集合的每一行插入一个新项LARAVEL



如何做到这一点,而不是使用循环&数组,而不是使用集合?

我有一个这样创建的集合

  $datas = collect($xml_datas['data']);

集合格式如下:

[
{
    "reference": "3437247",
    "numero_mandat": "4536",
    "type_mandat": "simple",
    "operation": "vente",
    "type": "Bureau ",
    "adresse": "Boulevard du chine ",
    "code_postal": "34000",
    "ville": "MONTPELLIER",
    "prix": "400000",
    "honoraire_agence": "0",
    "honoraire_frais_dossier": "0",
    "pourcentage_honoraire_acquereur": "5.26",
    "taxe_fonciere": "0.000000",
    "charges_mensuelles": "0",
    "surf_habitable": "180",
    "depot_garantie": null,
    "nombre_piece": "9",
    "annee_construction": "1977",
    "loyer_mensuel_occupant": "0.00"
},
{
    "reference": "3437271",
    "numero_mandat": "6125",
    "type_mandat": "simple",
    "operation": "vente",
    "type": "Maison de caractère",
    "adresse": "5 rue de l'égalité",
    "code_postal": "34800",
    "ville": "PERET",
    "prix": "803000",
    "honoraire_agence": "0",
    "honoraire_frais_dossier": "0",
    "pourcentage_honoraire_acquereur": "0",
    "taxe_fonciere": "2000.000000",
    "charges_mensuelles": "0",
    "surf_habitable": "210",
    "depot_garantie": null,
    "nombre_piece": "7",
    "annee_construction": "2000",
    "loyer_mensuel_occupant": "0.00"
},

我想在集合的每行中添加3个新项目(这些值是相同的),例如,这个集合有350个不同房地产属性的项目,我想在所有这350个项目中添加这些数据

       'software' => 'adaptimmo',
       'user_id' =>Auth::user()->id,
       'slug_import' => 'SomeRandom',

所以我的集合最终是这样的

[
{
    "software": "adaptimmo", // <--- We added
    "user_id": "1",
    "slug_import": "SomeRandom",
    "reference": "3437247",
    "numero_mandat": "4536",
    "type_mandat": "simple",
    "operation": "vente",
    "type": "Bureau ",
    "adresse": "Boulevard du chine ",
    "code_postal": "34000",
    "ville": "MONTPELLIER",
    "prix": "400000",
    "honoraire_agence": "0",
    "honoraire_frais_dossier": "0",
    "pourcentage_honoraire_acquereur": "5.26",
    "taxe_fonciere": "0.000000",
    "charges_mensuelles": "0",
    "surf_habitable": "180",
    "depot_garantie": null,
    "nombre_piece": "9",
    "annee_construction": "1977",
    "loyer_mensuel_occupant": "0.00"
},
{
    "software": "adaptimmo",
    "user_id": "1",
    "slug_import": "SomeRandom",
    "reference": "3437271",
    "numero_mandat": "6125",
    "type_mandat": "simple",
    "operation": "vente",
    "type": "Maison de caractère",
    "adresse": "5 rue de l'égalité",
    "code_postal": "34800",
    "ville": "PERET",
    "prix": "803000",
    "honoraire_agence": "0",
    "honoraire_frais_dossier": "0",
    "pourcentage_honoraire_acquereur": "0",
    "taxe_fonciere": "2000.000000",
    "charges_mensuelles": "0",
    "surf_habitable": "210",
    "depot_garantie": null,
    "nombre_piece": "7",
    "annee_construction": "2000",
    "loyer_mensuel_occupant": "0.00"
},

这里记录了许多集合可用的方法:http://laravel.com/docs/master/collections#available-methods

您可能正在寻找each()方法:

$items = collect($xml_datas['data'])->each(function ($item) {
    // Add new keys to the item
    $item->software = 'adaptimmo';
    $item->user_id = Auth::user()->id;
    $item->slug_import = 'SomeRandom';
});

最新更新