2016-09-06 64 views
0

我怎么能实现这个不使用循环&数组,而是使用一个集合?插入集合的每一行一个新项目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" 
}, 

回答

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'; 
}); 
+0

奥基刚刚尝试过我认为你正确的方式来做到这一点,但我有一个新的问题现在,从我所了解的集合不会改变你给出的'格式',因此,因为我的数据是阵列,我不能使用 - >每个$ item->软件,我得到关于不是对象的错误。我该如何解决这个问题? – Mister92

+0

使用数组语法,而不是数组:'$ item ['software'] ='adaptimmo''等 –