Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
turahe committed Sep 24, 2020
0 parents commit 4ac49fd
Show file tree
Hide file tree
Showing 14 changed files with 746 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/bootstrap/compiled.php
.env.*.php
.env.php
!empty
.idea
.project
.settings
.buildpath
composer.lock
/vendor
.phpintel/*
.phpunit.result.cache
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

php:
- 7.2
- 7.4

before_script:
- travis_retry composer self-update
- COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install

script: vendor/bin/phpunit --verbose

notifications:
email:
on_success: never
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Robert Conner <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Laravel Likeable Plugin
============

Important Note: As of version 1.2 I renamed `Conner\Likeable\LikeableTrait` to `Conner\Likeable\Likeable`

[![Build Status](https://travis-ci.org/rtconner/laravel-likeable.svg?branch=laravel-7)](https://travis-ci.org/rtconner/laravel-likeable)
[![Latest Stable Version](https://poser.pugx.org/rtconner/laravel-likeable/v/stable.svg)](https://packagist.org/packages/rtconner/laravel-likeable)
[![License](https://poser.pugx.org/rtconner/laravel-likeable/license.svg)](https://packagist.org/packages/rtconner/laravel-likeable)

Trait for Laravel Eloquent models to allow easy implementation of a "like" or "favorite" or "remember" feature.

[Laravel 5/6/7 Documentation](https://github.com/rtconner/laravel-likeable/tree/laravel-7)
[Laravel 4 Documentation](https://github.com/rtconner/laravel-likeable/tree/laravel-4)

#### Composer Install (for Laravel 5)

composer require rtconner/laravel-likeable "~3.0"

#### Then run the migrations

```bash
php artisan migrate
```

#### Setup your models

```php
class Article extends \Illuminate\Database\Eloquent\Model {
use \Conner\Likeable\LikeableTrait;
}
```

#### Sample Usage

```php
$article->like(); // like the article for current user
$article->like($myUserId); // pass in your own user id
$article->like(0); // just add likes to the count, and don't track by user

$article->unlike(); // remove like from the article
$article->unlike($myUserId); // pass in your own user id
$article->unlike(0); // remove likes from the count -- does not check for user

$article->likeCount; // get count of likes

$article->likes; // Iterable Illuminate\Database\Eloquent\Collection of existing likes

$article->liked(); // check if currently logged in user liked the article
$article->liked($myUserId);

Article::whereLikedBy($myUserId) // find only articles where user liked them
->with('likeCounter') // highly suggested to allow eager load
->get();
```

#### Credits

- Robert Conner - http://smartersoftware.net
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "turahe/laravel-likeable",
"description": "Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.",
"license": "MIT",
"keywords": ["trait", "laravel", "laravel5", "eloquent", "likeable", "likable", "like", "remember", "follow", "favorite", "favourite"],
"authors": [
{
"name": "Robert Conner",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6.0",
"illuminate/database": "^6.0|^7.0|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0"
},
"require-dev": {
"orchestra/testbench": "^5.0 | ^6.0",
"phpunit/phpunit": "^8.5|^9.3",
"mockery/mockery": "^1.3"
},
"autoload": {
"psr-4": {
"Turahe\\Likeable\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Conner\\Tests\\Likeable\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"Turahe\\Likeable\\LikeableServiceProvider"
]
}
},
"minimum-stability": "dev"
}
20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="sqlite"/>
</php>
</phpunit>
27 changes: 27 additions & 0 deletions src/Like.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Turahe\Likeable;

use Illuminate\Database\Eloquent\Model;

/**
* @mixin \Eloquent
* @property Likeable likeable
* @property string user_id
* @property string likeable_id
* @property string likeable_type
*/
class Like extends Model
{
protected $table = 'likes';
public $timestamps = true;
protected $fillable = ['likeable_id', 'likeable_type', 'user_id'];

/**
* @access private
*/
public function likeable()
{
return $this->morphTo();
}
}
48 changes: 48 additions & 0 deletions src/LikeCounter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Turahe\Likeable;

use Illuminate\Database\Eloquent\Model;

/**
* @mixin \Eloquent
* @property Likeable likeable
*/
class LikeCounter extends Model
{
protected $table = 'like_counters';
public $timestamps = false;
protected $fillable = ['likeable_id', 'likeable_type', 'count'];

/**
* @access private
*/
public function likeable()
{
return $this->morphTo();
}

/**
* Delete all counts of the given model, and recount them and insert new counts
*
* @param $modelClass
*/
public static function rebuild($modelClass)
{
if(empty($modelClass)) {
throw new \Exception('$modelClass cannot be empty/null. Maybe set the $morphClass variable on your model.');
}

$builder = Like::query()
->select(\DB::raw('count(*) as count, likeable_type, likeable_id'))
->where('likeable_type', $modelClass)
->groupBy('likeable_id');

$results = $builder->get();

$inserts = $results->toArray();

\DB::table((new static)->table)->insert($inserts);
}

}
Loading

0 comments on commit 4ac49fd

Please sign in to comment.