PHP code example of jobmetric / laravel-star

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

    

jobmetric / laravel-star example snippets


use JobMetric\Star\HasStar;

class Article extends Model
{
    use HasStar;
}

use JobMetric\Star\CanStar;

class User extends Model
{
    use CanStar;
}

$article->addStar(4, $user);

$article->addStar(5, null, ['device_id' => 'abc-123']);

$article->removeStar($user);
$article->removeStar(null, 'abc-123');

$article->hasStar($user); // true/false

$article->starCount(); // e.g., 10
$article->starAvg();   // e.g., 4.3

$article->starSummary(); 
// => collect([5 => 3, 4 => 5, 3 => 2])

$article->latestStars(5); // returns latest 5 stars

$article->forgetStars($user);

$article->isRatedAs(4, $user);     // true
$article->isRatedAbove(3, $user);  // true
$article->isRatedBelow(5, $user);  // true

$article->getRatedValue($user); // e.g., 4

$user->hasStarred($article); // true/false

$user->starredRate($article); // e.g., 5

$user->removeStarFrom($article);

$user->countStarGiven(5); // e.g., 2
$user->totalStarsGiven(); // e.g., 12

$user->starSummary(); 
// => collect([5 => 4, 3 => 2])

$user->starredItems(); // Collection of models
$user->starredItems(Article::class);

$user->starsToType(Article::class); // Collection of stars

$user->latestStarsGiven(5); // Collection of latest 5 stars
bash
php artisan migrate