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

时间:2016-09-06 12:12:23

标签: laravel eloquent

我如何才能实现这一目标,而不是使用循环和放大器数组而不是使用集合?

我有一个收藏女巫我这样创造

  $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"
},

1 个答案:

答案 0 :(得分:0)

此处记录的馆藏有许多方法: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';
});