Problem with favorites functionality in Laravel

Viewed 69

I am building favorites functionality where user can favorite certain image. I am using cybercog/laravel-love package. The error I am getting in postman when I try to favorite some image is

"message": "Reactant not exists.",
"exception": "Cog\\Contracts\\Love\\Reactant\\Exceptions\\ReactantInvalid",

I built the same functionality for like posts and it works fine but when I add favorite functionality I get this error. Also when I dd() it breaks in ReactionService.php on line where I put this code,

if($reacterFacade->hasReactedTo($reactant, $reactionType)) {

because $reactant doesn't exist.

Any help is appreciated. Here is my code.

Controller

public function profileImageFavorite(FavoriteRequest $request, ReactionService $reactionService, ProfileImage $profileImage)
{
    $user = Auth::user();
    $reactionService->favorite($user->profile, $profileImage, $request->favorite);

    return new ProfileImageResource($profileImage);
}

ReactionService.php

public function favorite($reacter, $reactant, $favorite)
{
    $reactionType = 'Favorite';
    $reacterFacade = $reacter->viaLoveReacter();
    if ($favorite == true) {
        if ($reacterFacade->hasReactedTo($reactant, $reactionType)) {

            dd('HERE IT BREAKS AND SHOWS ERROR');

            throw ValidationException::withMessages(['profileImage' => 'You already favorited this image']);
        } else {
            $reacterFacade->reactTo($reactant, $reactionType);
        }
    } else {
        if ($reacterFacade->hasNotReactedTo($reactant, $reactionType)) {
            throw ValidationException::withMessages(['profileImage' => 'You did not favorite this image']);
        } else {
            $reacterFacade->unreactTo($reactant, $reactionType);
        }
    }
}

ProfileImage.php

<?php

namespace App\Models;

use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableInterface;
use Cog\Laravel\Love\Reactable\Models\Traits\Reactable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class ProfileImage extends Model implements ReactableInterface
{
    use HasFactory, Reactable, Notifiable;

    protected $fillable = [
        'id',
        'user_profile_id',
        'title',
        'path',
        'url',
        'image_type',
        'love_reactant_id',
    ];
}

migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddLoveReactantIdToProfileImagesTable extends Migration
{
    public function up(): void
    {
        Schema::table('profile_images', function (Blueprint $table) {
            $table->unsignedBigInteger('love_reactant_id')->nullable();

            $table
                ->foreign('love_reactant_id')
                ->references('id')
                ->on('love_reactants');
        });
    }

    public function down(): void
    {
        Schema::table('profile_images', function (Blueprint $table) {
            $table->dropForeign(['love_reactant_id']);
            $table->dropColumn('love_reactant_id');
        });
    }
}

And also I have 'like' functionality for posts with the same code, just changed to be for post and like and it works fine.

0 Answers
Related