samedi 27 janvier 2018

How to eager load different morphs related model relations?

There was a rare situation, which is nowhere described and not googled:

class SomeModelFirst extends Model {
  public function notes()
  {
    return $this->morphMany(App\Note::class, 'noteable');
  }

  public function relationFirst()
  {
    return $this->belongsTo(Photo::class);
  }
}

class SomeModelSecond extends Model {
  public function notes()
  {
    return $this->morphMany(App\Note::class, 'noteable');
  }

  public function relationSecond() {
    return $this->belongsToMany(Comment::class);
  }
}

class Note extends Model {
  public function noteable()
  {
    return $this->morphTo();
  }
}

Table

notenables:
noteable_to | noteable_id 
SomeModelFirst | 1
SomeModelSecond | 1

And needle action

$notes = Notes::with('noteable', 'noteable.relationFirst', 'noteable.relationSecond');
// throws error, because SomeModelFirst doesn't have relationSecond and SomeModelSecond relationFirst.

Is it possible to somehow resolve this situation?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire