I have a model presenting a report from a user. The report model has polymorphic relationships that can contain either a recipe or a comment.
The goal is to be able to delete a comment or user and delete related reports eloquent.
With my current setting (see below) this will not work, when you delete a comment, the report remains and causes an error, because now it points to a nonexistent comment.
What am I doing wrong? Do I need a “belongs” relationship for my polymorphic model? If so, how can I build this relationship when the relationship is morphing?
Models
Polymorphic Model
class Report extends Model {
public function reportable() {
return $this->morphTo();
}
public function User() {
return $this->belongsTo('App\User');
}
}
Recipe Model
class Recipe extends Model {
public function user() {
return $this->belongsTo('App\User');
}
public function reports() {
return $this->morphMany('App\Report', 'reportable');
}
}
Comment Model
class RecipeComment extends Model {
public function user() {
return $this->belongsTo('App\User');
}
public function reports() {
return $this->morphMany('App\Report', 'reportable');
}
}