PHP code example of modularavel / likeable

1. Go to this page and download the library: Download modularavel/likeable 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/ */

    

modularavel / likeable example snippets


use Modularavel\Likeable\Traits\Likeable;

class Post extends Model
{
    use Likeable;

    // Your model code...
}

// Like/Dislike actions
$post->like();
$post->dislike();

// Get counts
$post->likesCount();
$post->dislikesCount();

// Check if liked/disliked by current user or IP
$post->isLikedBy();
$post->isDislikedBy();

// Relationships
$post->likes;
$post->dislikes;
$post->allLikes;

// Like a post
$post->like(); // Uses current authenticated user or IP

// Dislike a post
$post->dislike();

// Like/Dislike with specific user
$post->like($userId);
$post->dislike($userId);

// Get like count
$likesCount = $post->likesCount();
$dislikesCount = $post->dislikesCount();

// Check if user has liked
if ($post->isLikedBy()) {
    // User has liked this post
}

// Check if user has disliked
if ($post->isDislikedBy()) {
    // User has disliked this post
}

return [
    // User model to use
    'user_model' => App\Models\User::class,

    // Track guests by IP address
    'track_by_ip' => true,

    // Show counts on buttons
    'show_counts' => true,

    // Enable/disable dislikes
    'enable_dislikes' => true,
];
bash
php artisan vendor:publish --tag=likeable-migrations
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Modularavel\Likeable\LikeableServiceProvider" --tag=config
bash
php artisan vendor:publish --tag=likeable-views
bash
php artisan vendor:publish --tag=likeable-views