2017-07-13 68 views
0

我能夠顯示用戶數據(第二陣列)$post->user->name循環但我怎麼顯示第三個數組數據?它不工作爲$post->likes->user_id
請幫忙。
這裏是我的數組列表:laravel中的多維數組

[{ 
    "id": 13, 
    "user_id": 1, 
    "content": "\"The reason most people never reach their goals is that they don't define them, or ever seriously consider them as believable or achievable. Winners can tell you where they are going, what they plan to do along the way, and who will be sharing the adventure with them.\" --Denis Waitley", 
    "status": 0, 
    "created_at": "2017-07-06 04:02:41", 
    "updated_at": "2017-07-06 04:02:41", 
    "user": { 
     "id": 1, 
     "name": "hardeep singh", 
     "gender": "male", 
     "slug": "hardeep-singh", 
     "email": "[email protected]", 
     "pic": "47224964-profile-pictures.jpg", 
     "role": "admin", 
     "created_at": "2017-02-21 06:06:28", 
     "updated_at": "2017-02-21 06:06:28" 
    }, 
    "likes": { 
     "id": 2, 
     "posts_id": 13, 
     "user_id": 3, 
     "created_at": "2017-07-07 17:52:18" 
    } 
}, { 
    "id": 12, 
    "user_id": 10, 
    "content": "\"People who succeed have momentum. The more they succeed, the more they want to succeed, and the more they find a way to succeed. Similarly, when someone is failing, the tendency is to get on a downward spiral that can even become a self-fulfilling prophecy.\" --Tony Robbins", 
    "status": 0, 
    "created_at": "2017-07-06 04:00:14", 
    "updated_at": "2017-07-06 04:00:14", 
    "user": { 
     "id": 10, 
     "name": "Ogran Gysn", 
     "gender": "male", 
     "slug": "ogran-gysn", 
     "email": "[email protected]", 
     "pic": "ee.jpg", 
     "role": "company", 
     "created_at": "2017-03-10 10:48:19", 
     "updated_at": "2017-03-10 10:48:19" 
    }, 
    "likes": { 
     "id": 3, 
     "posts_id": 12, 
     "user_id": 3, 
     "created_at": "2017-07-07 17:52:33" 
    } 
}, { 
    "id": 11, 
    "user_id": 9, 
    "content": "\"Thinking should become your capital asset, no matter whatever ups and downs you come across in your life.\" --A.P.J. Abdul Kalam", 
    "status": 0, 
    "created_at": "2017-07-06 03:59:47", 
    "updated_at": "2017-07-06 03:59:47", 
    "user": { 
     "id": 9, 
     "name": "john lewis", 
     "gender": "male", 
     "slug": "john-lewis", 
     "email": "[email protected]", 
     "pic": "scs.png", 
     "role": "company", 
     "created_at": "2017-03-10 04:04:27", 
     "updated_at": "2017-03-10 04:04:27" 
    }, 
    "likes": { 
     "id": 17, 
     "posts_id": 11, 
     "user_id": 1, 
     "created_at": "2017-07-13 10:07:56" 
    } 
}, { 
    "id": 10, 
    "user_id": 3, 
    "content": "Happiness is a butterfly, which when pursued, is always beyond your grasp, but which, if you will sit down quietly, may alight upon you", 
    "status": 0, 
    "created_at": "2017-07-06 03:59:19", 
    "updated_at": "2017-07-06 03:59:19", 
    "user": { 
     "id": 3, 
     "name": "anita donal", 
     "gender": "female", 
     "slug": "anita-donal", 
     "email": "[email protected]", 
     "pic": "47354305-profile-pictures.jpg", 
     "role": null, 
     "created_at": "2017-02-21 06:07:39", 
     "updated_at": "2017-02-21 06:07:39" 
    }, 
    "likes": { 
     "id": 13, 
     "posts_id": 10, 
     "user_id": 1, 
     "created_at": "2017-07-13 10:01:43" 
    } 
}, { 
    "id": 9, 
    "user_id": 1, 
    "content": "Take up one idea. Make that one idea your life--think of it, dream of it, live on that idea. Let the brain, muscles, nerves, every part of your body, be full of that idea, and just leave every other idea alone.", 
    "status": 0, 
    "created_at": "2017-07-06 03:58:40", 
    "updated_at": "2017-07-06 03:58:40", 
    "user": { 
     "id": 1, 
     "name": "hardeep singh", 
     "gender": "male", 
     "slug": "hardeep-singh", 
     "email": "[email protected]", 
     "pic": "47224964-profile-pictures.jpg", 
     "role": "admin", 
     "created_at": "2017-02-21 06:06:28", 
     "updated_at": "2017-02-21 06:06:28" 
    }, 
    "likes": { 
     "id": 19, 
     "posts_id": 9, 
     "user_id": 1, 
     "created_at": "2017-07-13 10:08:51" 
    } 
}, { 
    "id": 3, 
    "user_id": 9, 
    "content": "have a nic day :)", 
    "status": 0, 
    "created_at": "2017-07-05 07:59:22", 
    "updated_at": "2017-05-09 08:56:59", 
    "user": { 
     "id": 9, 
     "name": "john lewis", 
     "gender": "male", 
     "slug": "john-lewis", 
     "email": "[email protected]", 
     "pic": "scs.png", 
     "role": "company", 
     "created_at": "2017-03-10 04:04:27", 
     "updated_at": "2017-03-10 04:04:27" 
    }, 
    "likes": { 
     "id": 1, 
     "posts_id": 3, 
     "user_id": 1, 
     "created_at": "2017-07-13 10:33:43" 
    } 
}, { 
    "id": 1, 
    "user_id": 3, 
    "content": "This is demo post", 
    "status": 0, 
    "created_at": "2017-07-04 18:25:14", 
    "updated_at": "-0001-11-30 00:00:00", 
    "user": { 
     "id": 3, 
     "name": "anita donal", 
     "gender": "female", 
     "slug": "anita-donal", 
     "email": "[email protected]", 
     "pic": "47354305-profile-pictures.jpg", 
     "role": null, 
     "created_at": "2017-02-21 06:07:39", 
     "updated_at": "2017-02-21 06:07:39" 
    }, 
    "likes": null 
}, { 
    "id": 2, 
    "user_id": 3, 
    "content": "this is post by me, i am anita donal", 
    "status": 0, 
    "created_at": "2017-03-23 16:14:50", 
    "updated_at": "-0001-11-30 00:00:00", 
    "user": { 
     "id": 3, 
     "name": "anita donal", 
     "gender": "female", 
     "slug": "anita-donal", 
     "email": "[email protected]", 
     "pic": "47354305-profile-pictures.jpg", 
     "role": null, 
     "created_at": "2017-02-21 06:07:39", 
     "updated_at": "2017-02-21 06:07:39" 
    }, 
    "likes": { 
     "id": 21, 
     "posts_id": 2, 
     "user_id": 1, 
     "created_at": "2017-07-13 10:36:30" 
    } 
}] 
+0

你定義它們 –

+0

之間的關係@MichaelChen是的,我確定他們之間的關係就是爲什麼它的未來在一起 – hardeepcoder

回答

1

在陣列不能使用 - >我假設你的數組是$ post對象。

$post->likes->user_id. 
    foreach($post as $row){ 
      $row['like']['user_id'] 
    } 
+0

但我使用VUE我們,所以我能夠像 post.user.name或顯示來自用戶數據$ post-> user-> name 但不能顯示爲贊 - $ post-> likes-> user_id post表示排爲您的 foreach($ posts as $ post){ $ post ['like'] ['user_id'] } 但不工作 – hardeepcoder