duai5344 2016-03-02 20:24
浏览 131
已采纳

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

I've been scouring google trying to figure out a solution. I'm getting the:

ErrorException in Model.php line 2755: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

error when I try to go to my posts index page, and a just can't figure out where it's coming from. I can get to the page when I redirect from creating a post, but after that I get the error. Supposedly it's supposed to come from not returning a relationship, but here's my relationships and code:

Post

public function category() 
{
    return $this->belongsTo('App\Category', 'category_id');
}

public function user()
{
    return $this->belongsTo('App\User', 'user_id');
}

User

public function roles() 
{
    return $this->belongsToMany('App\Role')->withTimestamps();
}
public function posts()
{
    return $this->hasMany('App\Post', 'user_id');
}

Role

 public function users() 
{
    return $this->belongsToMany('App\User');
}

Category

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

index.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

and post.blade.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>

</div>
  • 写回答

1条回答 默认 最新

  • dttl3933 2016-03-02 20:57
    关注

    Before opening the form in post.blade, the last check you do is:

    Auth::user()->isAdmin
    

    In the other file, you check:

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

    Add them where missing

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题