Setup Reactable

Comment model cannot receive reactions directly. It should delegate this job to related Reactant model.

Add Reactable interface & trait to your Comment model code and run artisan setup command to make a link with Reactant model.

Code Changes

  1. Declare that model implements Cog\Contracts\Love\Reactable\Models\Reactable contract.

  2. Use Cog\Laravel\Love\Reactable\Models\Traits\Reactable trait or implement each method of the contract by yourself.

As result you will have:

<?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\Model;

class Comment extends Model implements ReactableInterface
{
    use Reactable;
}

Database Changes

  1. Run set up reactable command.
php artisan love:setup-reactable --model="App\Models\Comment"

📘

Add --nullable flag if all models of this type must NOT be reactable:

$ php artisan love:setup-reactable --model="App\Models\Comment" --nullable

📘

Manual migration creation described in Custom Setup Migrations.

  1. Run migration.
php artisan migrate

Creating Reactant Models

If you are integrating package on already existing user base you need to register your Reactable models as Reactants.

php artisan love:register-reactants --model="App\Models\Comment"

This command will create Reactant model for each Comment model.

Adding query scopes to Model

📘

Note

This step is optional. Required only for Reactable Filtering and Reactable Ordering functionality.

This package comes with predefined query builder methods to use database query scopes.

To start using it in your Comment model you should create CommentQueryBuilder class.

<?php

namespace App\Models;

class CommentEloquentBuilder extends \Illuminate\Database\Eloquent\Builder
{
    use \Cog\Laravel\Love\Reactable\ReactableEloquentBuilderTrait;

    // Other User model local query scopes
}

Then in your Comment model register that it should be used instead of the default \Illuminate\Database\Eloquent\Builder class.

<?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\Model;

/**
 * @method static UserEloquentBuilder query()
 */
class Comment extends Model implements ReactableInterface
{
    use Reactable;
  
    public function newEloquentBuilder($query): CommentEloquentBuilder
    {
        return new UserEloquentBuilder($query);
    }
}