Laravel Morph relation - detatch / attatch

105 Views Asked by At

I have this relational models:

enter image description here

My main goal is to have built reactions on system so there could be related to N identities (eg: Article, Photo, News, ...)

Reactions schema:

public function up()
{
    Schema::create('reactions', function (Blueprint $table){
        $table->increments('id');
        $table->string('title', 50)->unique()->index();
        $table->string('show_text', 20);

        $table->smallInteger('ordering')->nullable()->default(null);
    });
}

Reactionables schema:

Schema::create('reactionables', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('reactor_id')->unsigned();
        $table->integer('reaction_id')->unsigned();

        $table->foreign("reactor_id")->references("id")->on("users");
        $table->foreign("reaction_id")->references("id")->on("reactions");

        $table->morphs('reactionable');

        $table->timestamp('created_at')->nullable();
    });

hasReactions trait (to apply on reactionables such as Post, Images, etc.):

/**
 * Get related reactions
 *
 * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
 */
public function reactions()
{
    return $this->morphToMany(Reaction::class, 'reactionable')
        ->withPivot(['reactionable_id', 'reactionable_type']);
}

isReactor trait (to apply on Users):

/**
 * React to given instance
 * 
 * @param Reactionable $reactionable
 * @param ReactionType $applied_reaction
 *
 * @return bool
 */
public function react(Reactionable $reactionable, ReactionType $applied_reaction)
{
    $reactionable->reactions()
        ->detach(
            $reactionable->reactions()->where('reactor_id', $this->getKey())->get(['reactions.id'])->toArray()
        );

    return $this->storeReaction($reactionable, $applied_reaction);
}

/**
 * Store reaction
 * 
 * @param Reactionable $reactionable
 * @param ReactionType $applied_reaction
 *
 * @return bool
 */
private function storeReaction(Reactionable $reactionable, ReactionType $applied_reaction)
{
    try {
        $reactionable->reactions()->attach(
            $applied_reaction->getKey(), [
                'reactor_id' => $this->getKey(),
                'created_at' => Carbon::now()
            ]
        );
        return true;
    } catch (\Throwable $exception) {
        return false;
    }
}

The main issue here is that sometimes (randomly, I couldn't figure it how it happens), a reaction for some random user will deletes all of other reactions.

I don't even know if this is the best solution for this problem - I don't want to apply strict relation between Reactions ~ Posts applying inverse relation, so that's why I apply for this.

0

There are 0 best solutions below