jeudi 7 septembre 2017

Using a polymorphic relation in a child model causes an infinite loop?

This question was already asked here but it received no answer. No I face the same problem but in laravel 5.4. I have a model Book, a model ReadingSession and a model Comment. A book has many reading sessions and has many comments but the reading session can also have comments. So I have my relations defined like this:

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();
}

These seems to create an infinite loop. Can anyone hint me on what I'm doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire