2017-12-21 32 views
1

私はpostgresデータベースを持っています。このテーブルには2つのカラム(主キーである 'id'と、タイトル、タイトル、およびタイトルを含むJSONBデータセットを格納する 'data'コンテンツなど)。Laravel BladeでPostgresからJSONデータを表示

現在のところ、テーブル全体または各データを表示することはできますが、個々のデータを「データ」、つまりタイトルから取得することはできません。

Route::get('/home', function() { 
    $articles = DB::table('articles')->get(); 
    $results = json_decode($articles, true); 
    return view('home', compact('articles', 'results')); 
}); 

home.blade.phpテンプレート:ここ

は私もJSONデコードとして、JSONなどのデータに引っ張って試してみたルートです。私は記事と結果を使ってタイトルを表示しようとしました。私が得ることができる最高のは、テーブル全体を表示しているが、私はタイトルを呼び出すことができていない:私は得ているエラーの

@foreach ($results as $result)    
    <div>{{ $result->title }}</div> 
@endforeach 

@foreach ($articles as $article)    
    <div>{{ $article->title }}</div> 
@endforeach 

@foreach ($results as $result)    
    <div>{{$result['data']}}</div> //this shows the whole json object, posted below 
@endforeach 

@foreach ($results as $result)    
    <div>{{$result['data']['title']}}</div> //I believe this should work, but I get Illegal String Offset 'title' as an error, 
@endforeach 

一部:

htmlspecialchars() expects parameter 1 to be string, array given 
Array to string conversion 
Illegal string offset 'title' 
Something about calling something that's not there 

ここで何JSONですオブジェクトは次のようになりますjson_decode後($結果[「データ」]上から):

{"url": "http://omgili.com/ri/.wHSUbtEfZQrTHDoKYSJbjrpQN.N5MJgWJskXd50cUpWKooC_zdZBj5IfjtQ82V5YE9KjMI9MkoEoWsmLqcSDiWUKMSrDShx9H3vPUjRQuW0sylmueXyZg--", "text": "Cable companies are ove....", "uuid": "8c43aa206860570df0a86ff11f619235dea6e2bf", "title": "Cable companies are looking for ways to limit password sharing", "author": "theverge.com", "rating": null, "thread": {"url": "http://omgili.com/ri/.wHSUbtEfZQrTHDoKYSJbjrpQN.N5MJgWJskXd50cUpWKooC_zdZBj5IfjtQ82V5YE9KjMI9MkoEoWsmLqcSDiWUKMSrDShx9H3vPUjRQuW0sylmueXyZg--", "site": "theverge.com", "uuid": "8c43aa206860570df0a86ff11f619235dea6e2bf", "title": "Cable companies are looking for ways to limit password sharing", "social": {"vk": {"shares": 0}, "gplus": {"shares": 0}, "facebook": {"likes": 0, "shares": 0, "comments": 0}, "linkedin": {"shares": 0}, "pinterest": {"shares": 0}, "stumbledupon": {"shares": 0}}, "country": "US", "published": "2017-12-20T18:17:00.000+02:00", "site_full": "www.theverge.com", "site_type": "news", "main_image": "https://cdn.vox-cdn.com/thumbor/wCruRyorIkyClceG2T4Q0BsYk7Y=/0x73:1020x607/fit-in/1200x630/cdn.vox-cdn.com/assets/4562901/theverge1_1020.jpg", "spam_score": 0, "title_full": "Cable companies are looking for ways to limit password sharing - The Verge", "domain_rank": 496, "site_section": "http://www.theverge.com/tech/rss/index.xml", "replies_count": 0, "section_title": "The Verge - Tech Posts", "site_categories": ["media"], "performance_score": 0, "participants_count": 1}, "crawled": "2017-12-20T18:29:59.008+02:00", "entities": {"persons": [{"name": "rutledge", "sentiment": "none"}, {"name": "tom rutledge", "sentiment": "none"}], "locations": [], "organizations": [{"name": "netflix", "sentiment": "none"}, {"name": "bloomberg", "sentiment": "none"}, {"name": "viacom", "sentiment": "none"}, {"name": "ubs", "sentiment": "none"}, {"name": "espn", "sentiment": "none"}]}, "language": "english", "published": "2017-12-20T18:17:00.000+02:00", "highlightText": "", "ord_in_thread": 0, "external_links": [], "highlightTitle": ""} 

答えて

2

あなたはJSONのCollectionオブジェクトの代わりに、Postgresのから雄弁リターンあなたのデータを照会するとき、あなたのコードに問題がされます文字列。したがって、行:

$results = json_decode($articles, true); 

は動作しません。 json_decode関数は文字列(UTF8エンコードされた文字列)でのみ動作します。

実際には$article->dataは解析されておらず、文字列のままであるため表示されるエラーがあります。

Array to string conversionおよびIllegal string offset文字列を配列として扱うと、エラーが発生します。

JSONデータを正しく解析/デコードするには、コレクションを通過して手動で変換する必要があります。あなたは正しく連想配列にデータをマッピングするためにEloquent\Collection::map機能を使用することができます。

$results = $articles->map(function($article){ 
    return [ 
     'id' => $article->id, 
     'data' => json_decode($article->data, true) 
    ]; 
}) 
+0

は完全な説明と答えてくれてありがとう! – Alteredorange

関連する問題