Câu hỏi này đã được hỏi here nhưng không nhận được câu trả lời. Bây giờ tôi phải đối mặt với cùng một vấn đề nhưng trong laravel 5.4. Tôi có một mô hình Book
, một mô hình ReadingSession
và một mô hình Comment
. Một cuốn sách có nhiều buổi đọc và có nhiều ý kiến nhưng phiên đọc cũng có thể có ý kiến. Vì vậy, tôi đã quan hệ của tôi được định nghĩa như thế này:Sử dụng quan hệ đa hình trong mô hình con gây ra vòng lặp vô hạn?
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();
}
Những dường như để tạo ra một vòng lặp vô hạn . Bất cứ ai có thể gợi ý cho tôi về những gì tôi đang làm sai?