我有一个postgres数据库,它有一个包含两列的表('id'是主键,'data',存储一个包含标题,内容等的JSONB数据集。)
目前,我只能显示整个表格或每个“数据”,但我无法从“数据”中提取个人数据,即标题。
这是路径,我尝试将数据作为json,以及json解码:
Route::get('/home', function () {
$articles = DB::table('articles')->get();
$results = json_decode($articles, true);
return view('home', compact('articles', 'results'));
});
home.blade.php模板。我已经尝试使用文章和结果来显示标题。我能得到的最好的是显示整个表格,但我无法打电话给标题:
@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_decode之后json对象的样子(来自上面的$ result ['data']):
{"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": ""}
答案 0 :(得分:2)
您的代码的问题是当您从Postgres查询数据时,Eloquent会返回一个Collection
对象而不是JSON字符串。因此,行:
$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)
];
})