Closed Duohao closed 5 years ago
Hello, @Duohao!
There are 2 ways to use Love Reacter API:
$user->getLoveReacter()
returns Reacter
model which requires to make calls to Internal API.
Read about Reacter in documentation$commentReactant = $comment->getReactant();
$reactionType = ReactionType::fromName('Like');
$user->getLoveReacter()->reactTo($commentReactant, $reactionType);
$user->viaLoveReacter()
returns a ReacterFacade
with simplified API which looks very similar to first one. It have same methods names but awaits your application specific types, so you don't need to directly operate with internal Love models.
Read about Reacter Facade in documentationBecause you've used viaLoveReacter
then there is no need to getReactant()
. Just pass $comment
instance to reactTo
method. And `ReactionType::fromName()' could be just a string.
$user->viaLoveReacter()->reactTo($comment, 'Like');
Your example could be rewritten this way:
$user = \Hello\Models\User\User::find(3);
if ($user->isNotRegisteredAsLoveReacter()) {
$user->registerAsLoveReacter();
}
$comment = \Hello\Models\Comments\Comments::find(2);
if ($comment->isNotRegisteredAsLoveReactant()) {
$comment->registerAsLoveReactant();
}
$user->viaLoveReacter()->reactTo($comment, 'Like');
I think that you don't need to register user & comments after the find. Registering better do in background to not affect client execution process.
If you are installing package in already deployed system, then you'd better create console command which will seed your database: example of command
At the end your controller code will be very clean & simple.
$user = \Hello\Models\User\User::find(3);
$comment = \Hello\Models\Comments\Comments::find(2);
$user->viaLoveReacter()->reactTo($comment, 'Like');
Thanks for your patience, it's ok now :
$user = \Hello\Models\User\User::find(4);
$comment = \Hello\Models\Comments\Comments::find(2);
if ($user->isNotRegisteredAsLoveReacter()) {
$user->registerAsLoveReacter();
}
if ($comment->isNotRegisteredAsLoveReactant()) {
$comment->registerAsLoveReactant();
}
if ($user->viaLoveReacter()->hasNotReactedTo($comment, 'Like')) {
$user->viaLoveReacter()->reactTo($comment, 'Like');
}
document has no example , diffcult to get error reason
code:
error :