cybercog / laravel-love

Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
https://komarev.com/sources/laravel-love
MIT License
1.17k stars 71 forks source link

Reactant not exists. #182

Closed Marchiuzzz closed 3 years ago

Marchiuzzz commented 3 years ago

Hi, I have followed the setup guidelines to get this package installed for my existing project with already existing models and records. However, I ran into a problem I cannot seem to solve. Whenever I try to use "$reacterFacade->reactTo" I get this "Reactant not exists." error, I have registered reacterers as well as reactants manually, as suggested in https://github.com/cybercog/laravel-love/issues/105 , but that did not solve the issue. Here's my simple function

public function like(Request $request){
      if(Auth::check()){
        $user = Auth::user();
        $object = DynamicAbstractObject::where('unique_entity_id', $request->input('entity'))->first();
        if(!empty($object)){
          if($object->isNotRegisteredAsLoveReactant()){
            $object->registerAsLoveReactant();
            $object->save();
          }
          if($user->isNotRegisteredAsLoveReacter()){
            $user->registerAsLoveReacter();
            $user->save();
          }
          $reacterFacade = $user->viaLoveReacter();
          $reacterFacade->reactTo($object, 'Like', 4.0);
        }
      }
    }

image

Any help would be much appreciated

antonkomarev commented 3 years ago

Do you have records in love_reactants database table? There is no need to create reactants on the fly on each like call. It should be done once for each model on initial package integration: https://laravel-love.readme.io/v8/docs/restore-missing-reactants

Marchiuzzz commented 3 years ago

I found what the problem was. I ignored the fact that when executing this step of setup "php artisan love:setup-reactable --model="App\Comment" I ran the migration and the column was created but foreign key failed to create. All I had to do was run "php artisan love:setup-reactable --model="App\Comment --nullable" instead and then repeat the manual registration. Everything works now, thanks for this wonderful package.