2017-03-02 37 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(); 

給這個一去,如果你的作品試圖完成什麼。