Laravel Love

repository·master·Indexed 22 days ago

https://github.com/cybercog/laravel-love

A Laravel package that enables emotional reactions (similar to GitHub, Facebook, or YouTube) for any Eloquent model. It provides tools for managing reaction types, registering reactable and reacterable models, and rebuilding reaction aggregates via Artisan commands. The package includes comprehensive migration paths for upgrading between versions v5 through v10.

Tokens
3.3K
Snippets
6
Records
21
Agent score
79%

What's inside laravel-love

  1. Upgrade from v8 to v9: Custom Eloquent Builder for Scopes

    master

    In v9, Reactable trait methods like scopeWhereReactedBy, scopeWhereNotReactedBy, scopeJoinReactionCounterOfType, and scopeJoinReactionTotal were moved to ReactableEloquentBuilderTrait.

    To continue using these scopes, you must create a custom Eloquent Builder class, use the trait within it, and override the newEloquentBuilder method in your model.

    /**
     * @method static UserEloquentBuilder query()
     */
    class User extends Model
    {
        public function newEloquentBuilder($query): UserEloquentBuilder
        {
            return new UserEloquentBuilder($query);
        }
    }
    
    class UserEloquentBuilder extends \Illuminate\Database\Eloquent\Builder
    {
        use \Cog\Laravel\Love\Reactable\ReactableEloquentBuilderTrait;
    
        // Other User model local query scopes
    }
  2. Upgrade from v7 to v8: API and Database Changes

    master

    Upgrading from v7 to v8 involves several breaking changes to method names and data types:

    Method Renames and Changes

    • Weights: All weight values are now float. Round them manually if you require integer values.
    • Reaction Checks:
      • Replace isReactedTo and isReactedToWithType with hasReactedTo.
      • Replace isNotReactedTo and isNotReactedToWithType with hasNotReactedTo.
      • Replace isReactedByWithType with isReactedBy.
      • Replace isNotReactedByWithType with isNotReactedBy.
    • Reaction Types:
      • ReactionType::getWeight() is renamed to getMass().
    • Query Scopes:
      • Replace whereReactedByWithType with whereReactedBy.
      • whereReactedBy now expects the first argument to be the Reacterable and the second to be the string name of the ReactionType.
      • joinReactionCounterOfType now expects the ReactionType as a string name.

    Virtual Attribute Prefix Changes

    • joinReactionCounterOfType prefix changed from reactions_ to reaction_{$type}_.
    • joinReactionTotal prefix changed from reactions_total to reaction_total_.

    Database Migration

    IMPORTANT: Create a backup of your production database before proceeding.

    Run the automated migration command (requires doctrine/dbal):

    php artisan love:upgrade-v7-to-v8
    php artisan love:upgrade-v7-to-v8
  3. Upgrade from v6 to v7: Facades and Contracts

    master

    If you are using the Love facade, you must switch to the new specialized facades. The global Cog\Laravel\Love\Facades\Love facade has been removed.

    New Facades:

    • Cog\Laravel\Love\Reacter\Facades\Reacter
    • Cog\Laravel\Love\Reactant\Facades\Reactant

    Contract Implementation: If you implement Reactable or Reacterable contracts manually (without using packaged traits), you must implement these two new methods:

    • Reactable models must implement viaLoveReactant.
    • Reacterable models must implement viaLoveReacter.
  4. Upgrade from v9 to v10

    master

    When upgrading to v10, note the following breaking changes:

    • Removed Observers: ReactionCounterObserver, ReactionTotalObserver, and the ReactionObserver::creating event have been removed.
    • Model Defaults: Default values for ReactionCounter, ReactionTotal, and Reaction models are now defined directly inside the models. If you use the package's default models, this is not a breaking change.
    • Unguarded Models: All package models are now unguarded. If you pass values to them without validation, you must refactor your code to ensure data integrity.
  5. Upgrade from v8 to v9: Recount Command Queue Connection

    master

    The love:recount command no longer uses the sync connection by default; it now uses the value defined in your queue.default configuration.

    To force a synchronous statistics recount, use the --queue-connection=sync option.

    php artisan love:recount --model="App\User" --queue-connection=sync
  6. Upgrade from v5 to v6: Refactoring Guide

    master

    v6 is a major refactor. Most changes cannot be automated due to API differences.

    Model Mapping

    • Replace Cog\Contracts\Love\Likeable\Models\Likeable $\rightarrow$ Cog\Contracts\Love\Reactable\Models\Reactable
    • Replace Cog\Laravel\Love\Likeable\Models\Traits\Likeable $\rightarrow$ Cog\Laravel\Love\Reactable\Models\Traits\Reactable
    • Replace Cog\Contracts\Love\Liker\Models\Liker $\rightarrow$ Cog\Contracts\Love\Reacterable\Models\Reacterable
    • Replace Cog\Laravel\Love\Liker\Models\Traits\Liker $\rightarrow$ Cog\Laravel\Love\Reacterable\Models\Traits\Reacterable

    Database Requirements

    Add the following columns to every table that models can react on/to:

    • $table->unsignedBigInteger('love_reacter_id'); (for models that can react)
    • $table->unsignedBigInteger('love_reactant_id'); (for models that can be reacted upon)

    Method Mapping

    • whereLikedBy / whereDislikedBy $\rightarrow$ whereReactedByWithType
    • like / dislike $\rightarrow$ reactTo
    • unlike / undislike $\rightarrow$ unreactTo
    • orderByLikesCount / orderByDislikesCount $\rightarrow$ use joinReactionCounterOfType combined with a standard orderBy.

    Database Migration

    IMPORTANT: Create a backup of your production database before proceeding.

    Run the automated migration command:

    php artisan love:upgrade-v5-to-v6
    php artisan love:upgrade-v5-to-v6
  7. Upgrade Laravel Love from v5 to v6

    master

    When upgrading the Laravel Love package from version 5 to version 6, use the love:upgrade-v5-to-v6 Artisan command. This command automates the migration of your database schema and data from the old 'likes' system to the new 'reactions' system.

    The command performs the following steps:

    1. Database Migration: Runs standard Laravel migrations.
    2. Populate Reaction Types: Converts old like types (e.g., 'Like', 'Dislike') into ReactionType records.
    3. Populate Reacters: Creates Reacter records for your user models (based on your default auth guard).
    4. Populate Reactants: Creates Reactant records for your models that were previously likeable.
    5. Convert Likes to Reactions: Migrates existing data from the love_likes table into the new reactions table.
    6. Cleanup: Deletes old database tables (love_likes, love_like_counters) and removes obsolete migration files.
  8. Options for love:setup-reacterable

    master

    The love:setup-reacterable command supports the following options:

    • --model: The fully qualified class name of the model you want to make reacterable (e.g., App\Models\Post). If omitted, you will be prompted to enter the name.
    • --not-nullable: If this flag is present, the generated migration will create the love_reacter_id foreign column as NOT NULL. If the flag is absent, the column will be nullable.
  9. Options for love:register-reactants

    master

    The love:register-reactants command accepts the following options:

    • --model: Required. The fully qualified class name of the Reactable model (e.g., App\Models\Post). If you use a morph map, you can provide the morph map alias instead.
    • --ids: Optional. A comma-separated list of specific model IDs to register (e.g., --ids=1,2,16,34). If this option is omitted, the command will attempt to register all unregistered models of the specified type.