Laravel 5 此集合实例上不存在属性[body]
当我试图基于slug从数据库中获取数据时,会出现此错误 这是我的控制器Laravel 5 此集合实例上不存在属性[body],laravel-5,slug,Laravel 5,Slug,当我试图基于slug从数据库中获取数据时,会出现此错误 这是我的控制器 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Post; class BlogController extends Controller { public function getSingle($slug) { // Fetch from the DB based on slug
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class BlogController extends Controller
{
public function getSingle($slug) {
// Fetch from the DB based on slug
$post = Post::where('slug', '=', $slug)->get();
// Return the view and pass in the post object
return view('blog.single')->with('post', $post);
}
}
您在$post
中获得数据,这是一种数组类型,您使用的是对象类型如果在刀片服务器中使用for循环,那么它将正常工作
@extends('main')
@section('title', '| {{ $post->title }}')
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>hi</h1>
@if($post)
@foreach($post as $key =>$value)
<p>{{ $value->body }}</p>
@endforeach
@endif
</div>
</div>
@endsection
@extends('main'))
@节('title','{{$post->title}}'))
@节(“内容”)
你好
@如果有的话(员额)
@foreach($key=>$value)
{{$value->body}
@endforeach
@恩迪夫
@端部
希望这将帮助您在视图中执行{{dd($post)}},并告诉我们您得到了什么输出。它正确地显示了标题,但对于标记,显示的是:Collection{178▼ #items:[]}实际上我尝试过这个解决方案,但当我刷新它时,它只显示标题,而不显示段落。当您刷新页面时,您可以检查您得到了什么响应。请补充你的回答
@extends('main')
@section('title', '| {{ $post->title }}')
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>hi</h1>
@if($post)
@foreach($post as $key =>$value)
<p>{{ $value->body }}</p>
@endforeach
@endif
</div>
</div>
@endsection