這個問題已經被問到here,但它沒有收到任何答案。現在我面臨同樣的問題,但是在laravel 5.4中。我有一個型號Book
,型號ReadingSession
和型號Comment
。一本書有許多閱讀會議,並有許多意見,但閱讀會議也可以有評論。所以,我有我的關係定義如下:在子模型中使用多態關係導致無限循環?
book.php中
protected $with = [
'author',
'readingSessions',
'userRating',
'ratings',
'comments'
];
public function users()
{
return $this->belongsToMany(User::class, 'user_book');
}
public function author()
{
return $this->belongsTo(Author::class);
}
public function allReadingSessions()
{
return $this->hasMany(ReadingSession::class);
}
public function readingSessions()
{
return $this->hasMany(ReadingSession::class)
->where('user_id', Auth::user()->id);
}
public function ratings()
{
return $this->hasMany(Rating::class);
}
public function userRating()
{
return $this->hasMany(Rating::class)
->where('user_id', Auth::user()->id);
}
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable');
}
ReadingSession.php
protected $with = ['comments'];
public function user()
{
return $this->belongsTo(User::class);
}
public function book()
{
return $this->belongsTo(Book::class);
}
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable');
}
Comment.php
public function commentable()
{
return $this->morphTo();
}
這些似乎產生了一個無限循環。任何人都可以暗示我我做錯了什麼?