Laravel removing polymorphic relationships, possibly having wrong relationships

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');
    }
}
+4
1

Laravel . , . , deleting, .

A Laravel 5 :

class RecipeCommentObserver {
    public function deleting($model) {
        try {
            DB::transaction(function() use ($model) {
                /**
                 * Try to delete the necessary related objects when this object is deleted.
                 */

                // detach the many-manys from this model, which will delete
                // the records in the pivot table.
                // e.g. if you had a many-many relationship named 'tags':
                // $model->tags()->detach();

                // the one-one and one-many relations to try and delete
                // for your example, you probably only want 'reports' here.
                // you probably don't to delete the user when deleting a comment.
                $relations = ['reports'];

                foreach ($relations as $relation) {
                    // get a list of all the related ids for this relation
                    $ids = $model->$relation()->lists('id');

                    // use the ->destroy method so any events get fired for the deleted objects
                    // if the amount deleted is less than expected, an error occurred
                    if (!empty($ids) && $model->$relation()->getRelated()->destroy($ids) < count($ids)) {
                        throw new Exception('Error occurred deleting ' . $relation);
                    }
                }
            });
        } catch (Exception $e) {
            throw $e;
        }
    }
}

boot() app/Providers/EventServiceProvider.php:

public function boot(DispatcherContract $events) {
    parent::boot($events);

    RecipeComment::observe(new RecipeCommentObserver());
}
+1

Source: https://habr.com/ru/post/1584066/


All Articles