PHP code example of devdojo / laravel-reactions

1. Go to this page and download the library: Download devdojo/laravel-reactions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devdojo / laravel-reactions example snippets


DevDojo\LaravelReactions\Providers\ReactionsServiceProvider::class,

use DevDojo\LaravelReactions\Traits\Reacts;

class User extends Model {
    use Reacts;
}

use DevDojo\LaravelReactions\Traits\Reactable;
use DevDojo\LaravelReactions\Contracts\ReactableInterface;

class Post extends Model implements ReactableInterface {
    use Reactable;
}

$likeReaction = Reaction::createFromName('like');
$likeReaction->save();

$loveReaction = Reaction::createFromName('love');
$loveReaction->save();

// picking the first user, for this example...
$user = User::first();

// the previously created reaction
$likeReaction = Reaction::where('name', '=', 'like')->first();

// picking up a post...
$awesomePost = Post::first();

// react to it!
$user->reactTo($awesomePost, $likeReaction);

// picking up a post...
$awesomePost = Post::first();

// get all reactions
$reactions = $awesomePost->reactions;

// picking up a post...
$awesomePost = Post::first();

// get a summary of related reactions
$reactionsSummary = $awesomePost->getReactionsSummary();

$reactionsSummary = $awesomePost->getReactionsSummary();
return $reactionsSummary;

// our awesome post.
$awesomePost = Post::first();

// every $reaction is a Reaction model
foreach($awesomePost->reactions as $reaction) 
{
    $user = $reaction->getResponder();
   
    // this will output "Francesco"
    echo $user->name;
}
bash
$ php artisan migrate