我一直在谷歌上搜索,试图找出一个解决方案。我得到:
ErrorException in Model.php line 2755: Relationship method must return an object of type Illuminate'Database'Eloquent'Relations'Relation
当我尝试转到我的帖子索引页面时出错,并且无法弄清楚它来自哪里。当我从创建帖子重定向时,我可以进入该页面,但之后我收到错误。据说它应该来自不返回关系,但这是我的关系和代码:
发布
public function category()
{
return $this->belongsTo('App'Category', 'category_id');
}
public function user()
{
return $this->belongsTo('App'User', 'user_id');
}
用户
public function roles()
{
return $this->belongsToMany('App'Role')->withTimestamps();
}
public function posts()
{
return $this->hasMany('App'Post', 'user_id');
}
角色
public function users()
{
return $this->belongsToMany('App'User');
}
类别
public function posts()
{
return $this->hasMany('App'Post', 'category_id');
}
索引刀片.php
@extends('app')
@section('content')
<h1>Posts</h1>
@if (Auth::check())
@if (Auth::user()->isAdmin())
<a href="/post/create">Create a Post</a>
@endif
@endif
@foreach ($posts as $post)
@include('includes.post', ['post' => $post, 'link' => true])
@endforeach
@stop
和后刀片.php
<div class="post">
<div class="div">
<div class="postCategory">
</div>
@if($link)
<a href="/post/{{ $post->id }}"><h2>{{ $post->title }}</h2></a>
@else
<h2>{{ $post->title }}</h2>
@endif
<p>{!! $post->body !!}</p>
<p>{{ $post->category->name }}</p>
<p>Posted by <a href="/user/{{ $post->user->username }}">{{ $post->user->username }}</a> at {{ $post->created_at }}</p>
@if(Auth::check())
@if($post->user->username == Auth::user()->username || Auth::user()->isAdmin)
{!! Form::open(['method' => 'delete', 'route' => ['post.destroy', $post->id]]) !!}
<input type="submit" value="Delete Post" onclick="return confirm('Are you sure?')"></input>
{!! Form::close() !!}
@endif
@endif
</div>
</div>
在 post.blade 中打开表单之前,您所做的最后一项检查是:
Auth::user()->isAdmin
在另一个文件中,您检查:
Auth::user()->isAdmin() //note the ()
在缺少的位置添加它们