2017-03-02 35 views
0

我试图通过子表列通过故事表从列表中获取数据。我已经设法与作者合作,但我认为我必须错过某些东西或遗忘某些东西,因为它不再有用。我收到此错误此收集实例上不存在属性[注释]

Collection.php行1498中的异常:此集合实例上不存在属性[注释]。

我的控制器功能

public function slug($slug){ 
    $menus_child = Menu::where('menu_id', 0)->with('menusP')->get(); 
    $stories = Story::where('slug', $slug)->get(); 

    $slug = $slug; 

    // I used this to get the author's email going through the slug from 
    // story table 
    $story = Story::with('author')->where('slug', $slug)->first(); 
    $name = $story->author->first()->email; 

    // I would like to get the name in the comment table by going through 
    // the slug from the story table 
    $comment = Story::with('comment')->where('slug', $slug)->get(); 
    $test = $comment->comment->first()->name; 

    return view('open::public.single-story', compact('menus_child', 'stories', 'slug')); 
} 

我Comment.php

namespace App\Modules\Authors\Models; 

use Illuminate\Database\Eloquent\Model; 

class Comment extends Model 
{ 
    protected $fillable = array('name', 'comment'); 

    protected $guarded = array('id'); 

    protected $table = 'comments'; 

    //Validation rules 
    public static $rules = array(
     'name' => '', 
     'comment' => '' 
    ); 

    public function story(){ 
     return $this->belongsToMany('App\Modules\Authors\Models\Story', 'comment_story', 'comment_id', 'story_id'); 
    } 
} 

我Story.php

class Story extends Model 
{ 
    protected $fillable = array('title', 'content', 'type', 'slug'); 

    protected $guarded = array('id'); 

    protected $table = 'story'; 

    //Validation rules 
    public static $rules = array(
     'title' => '', 
     'content' => '', 
     'type' => '' 
    ); 

    public function slug($title){ 
     $this->attributes['title'] = $title; 
     $this->attributes['slug'] = Str::slug($title); 
    } 

    public function author(){ 
     return $this->belongsToMany('App\Modules\Authors\Models\Author', 'author_story', 'story_id', 'author_id'); 
    } 

    public function comment(){ 
     return $this->belongsToMany('App\Modules\Authors\Models\Comment', 'comment_story', 'story_id', 'comment_id'); 
    } 
} 
+0

你的关系是否正确?他们都被设置为'belongsToMany()'。我期望一个Story有一个作者('belongsTo()')和许多评论('hasMany()'),相反地一个评论有一个Story('belongsTo()')。 – Mei

回答

3

在dparoli的帮助下,我设法做到了。我不能相信这么长时间才能弄清楚。

我改变

$comment = Story::with('comment')->where('slug', $slug)->get(); 
$test = $comment->comment->first()->name; 

$comment = Story::with('comment')->where('slug', $slug)->first(); 
$test = $comment->comment->toArray(); 

和我加入测试

return view('open::public.single-story', compact('menus_child', 'stories', 'slug', 'test')); 

,然后加入

@foreach($test as $t) 
    <h1>{!! $t['name'] !!}</h1> 
    <p>{!! $t['comment'] !!} 
@endforeach 

在我看来

+0

很高兴知道你找到了满意的解决方案,做得很好。 – dparoli

0

试图改变这一点:

//here you get a collection of stories 
$comment = Story::with('comment')->where('slug', $slug)->get(); 

要这样:

//here you get a single story 
$comment = Story::with('comment')->where('slug', $slug)->first(); 
+0

那么我该如何显示与该故事相关的所有评论? – Isis

+0

也许@dparoli是对的。首先尝试他的解决方案,然后检查'comment'是否返回对象或集合。像'dd($ comment)'。原因是'first()'与故事链接,而不是评论。 – EddyTheDove

+0

'$ comment = Story :: with('comment') - > where('slug',$ slug) - > first();'显示故事。但我似乎无法得到所有的评论来显示 – Isis

0

我给你一个不同的方法来尝试

$comments = Comment::with(['story' => function ($query) { 
    $query->where('slug', '=', $slug); 
}])->get(); 

给这个一去,如果你的作品试图完成什么。