我正在使用laravel,在下面的演示中,出现了问题,数据无法在视图中显示:
演示:
article.blade.php
@foreach ($articles as $article)
{{$article}}
@endforeach
导致浏览器:
{"id":1,"title":"hello world","content":"demo","created_at":"2017-07-23 15:34:52","updated_at":"2017-07-23 15:34:55"}
但它有以下错误:
article.blade.php
@foreach ($articles as $article)
{{$article->title}}
@endforeach
错误:
Property [title] does not exist on this collection instance.
为什么会这样?
更新
dd($articles)
的结果:
我需要这个数据结构,不要修改它,外部数组可以被mofidied到一个集合。
array:2 [▼
0 => Collection {#485 ▼
#items: array:6 [▼
"id" => 1
"title" => "hello"
"content" => "hello world"
"user_id" => 2
"tags" => Collection {#505 ▼
#items: array:2 [▼
0 => Tag {#506 ▶}
1 => Tag {#508 ▶}
]
}
"created_at" => "2017-07-23 15:34:52"
"updated_at" => "2017-07-23 15:34:55"
]
}
1 => Collection {#500 ▼
#items: array:6 [▼
"id" => 2
"title" => "foo"
"content" => "foo bar"
"user_id" => 2
"tags" => Collection {#505 ▼
#items: array:2 [▼
0 => Tag {#506 ▶}
1 => Tag {#508 ▶}
]
}
"created_at" => "2017-07-23 16:27:43"
"updated_at" => "2017-07-23 16:27:46"
]
}
]
答案 0 :(得分:0)
您可以尝试使用
@foreach ($articles as $a)
{{$a->title}}
@endforeach
我知道这就像一个奇迹,但有时候,它就像一个魅力。