Laravel Relationship方法必须返回一个Relation类型的对象

时间:2016-03-02 20:24:58

标签: php laravel laravel-5

我一直在搜索谷歌试图找出解决方案。我得到了:

.updatepager(false, true)
当我尝试访问我的帖子索引页面时出现

错误,并且无法确定它来自哪里。当我从创建帖子重定向时,我可以访问该页面,但之后我收到错误。据说它应该来自不回归关系,但这是我的关系和代码:

发布

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');
}

index.blade.php



public function posts()
{
    return $this->hasMany('App\Post', 'category_id');
}




和post.blade.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
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

在post.blade中打开表单之前,最后一项检查是:

Auth::user()->isAdmin

在另一个文件中,您检查:

Auth::user()->isAdmin() //note the ()

将它们添加到缺少的地方