こんにちは
現在Laravelでアプリを作成していてWebapiを叩き取得したJsonをデコードして
viewに値を渡してforeeachで回して値を取り出そうとしているのですが、
値が取得できずTrying to get property of non-object
とエラーが出力されます。
$results
の値はコンソールに出力されているのでforeachが原因かと思います。
どのようにすれば値を取得できますか。よろしくお願いします。
JSON
1{ 2 "aggregation": { 3 "totalCount": 195324, 4 "skip": 0, 5 "limit": 5 6 }, 7 "items": [ 8 { 9 "id": "12345", 10 "title": "Harajuku jacket", 11 "categoryId": 1, 12 "imageUrl": "https://example.com", 13 "detailUrl": "https://www.example.com", 14 "lotSize": 1, 15 "lotUnit": "pieces", 16 "price": { 17 "currency": "JPY", 18 "value": "50" 19 }, 20 "ratings": 0, 21 "orders": 0, 22 "freight": { 23 "price": { 24 "currency": "USD", 25 "value": "0.0" 26 }, 27 "type": "Standard Shipping" 28 }, 29 "seller": { 30 "storeName": "Example Store", 31 "positiveFeedback": 1512, 32 "totalFeedback": 1809 33 }, 34 "freightTypes": [ 35 { 36 "type": "Test" 37 }, 38 { 39 "type": "Test" 40 }, 41 { 42 "type": "Test" 43 }, 44 { 45 "type": "Test" 46 }, 47 { 48 "type": "Test" 49 }, 50 { 51 "type": "Test" 52 }, 53 { 54 "type": "Test" 55 }, 56 { 57 "type": "Test" 58 }, 59 { 60 "type": "Test" 61 } 62 ] 63 } 64 ] 65}
searchController
1 public function search(Request $request) 2 { 3 $url = "http://example.com/search"; 4 $apiKey = 'API'; 5 $text = $request -> get('name'); 6 7 $data = [ 8 "text" => $text, 9 "currency" => "JPY", 10 "skip" => 0, 11 "limit" => 1, 12 "scrollPagination" => false 13 ]; 14 15 $header = [ 16 'X-Api-Client-Id:'.$apiKey, 17 'Content-Type:application/json', 18 ]; 19 $curl = curl_init(); 20 curl_setopt($curl,CURLOPT_URL, $url); 21 curl_setopt($curl,CURLOPT_CUSTOMREQUEST, 'POST'); 22 curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($data)); 23 curl_setopt($curl,CURLOPT_HTTPHEADER,$header); 24 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 25 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 26 curl_setopt($curl, CURLOPT_HEADER, true); 27 28 $response = curl_exec($curl); 29 30 $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 31 $header = substr($response, 0, $header_size); 32 $body = substr($response, $header_size); 33 $results = json_decode($body, true); 34 curl_close($curl); 35 return view('searchResult',compact('results')); 36 }
View
1<div class="container"> 2 <div class="row justify-content-center p-4"> 3 @foreach($results['items'] as $item) 4 <div class="col-md-3"> 5 <div class="card mb-3" style="width: 16rem;"> 6 <img class="card-img-top" src="{{$item ->imageUrl}}" alt="Card image"> 7 <div class="card-body"> 8 <h6 class="card-title font-weight-bold">{{$item ->title}}</h6> 9 <h5 class="card-text">{{$item -> value}}</h5> 10 <p class="card-text">by : <a href="#">{{$item->storeName}}</a></p> 11 <button type="submit" class="btn btn-primary">Add list</button> 12 </div> 13 </div> 14 </div> 15 @endforeach 16 </div> 17 </div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/12 10:04