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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了