Skip to content

Commit

Permalink
Separate migrations & Implementing ability to like models (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki authored Jul 20, 2018
1 parent 0a3b6e7 commit 71ae278
Show file tree
Hide file tree
Showing 16 changed files with 408 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Befriended extends Migration
class Followers extends Migration
{
/**
* Run the migrations.
Expand All @@ -24,18 +24,6 @@ public function up()

$table->timestamps();
});

Schema::create('blockers', function (Blueprint $table) {
$table->increments('id');

$table->integer('blockable_id');
$table->string('blockable_type');

$table->integer('blocker_id')->nullable();
$table->string('blocker_type')->nullable();

$table->timestamps();
});
}

/**
Expand All @@ -46,6 +34,5 @@ public function up()
public function down()
{
Schema::dropIfExists('followers');
Schema::dropIfExists('blockers');
}
}
38 changes: 38 additions & 0 deletions database/migrations/2018_07_14_183254_blockers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Blockers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('blockers', function (Blueprint $table) {
$table->increments('id');

$table->integer('blockable_id');
$table->string('blockable_type');

$table->integer('blocker_id')->nullable();
$table->string('blocker_type')->nullable();

$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('blockers');
}
}
38 changes: 38 additions & 0 deletions database/migrations/2018_07_14_183255_likers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Likers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('likers', function (Blueprint $table) {
$table->increments('id');

$table->integer('likeable_id');
$table->string('likeable_type');

$table->integer('liker_id')->nullable();
$table->string('liker_type')->nullable();

$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('likers');
}
}
47 changes: 47 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,53 @@ $user->isBlocking($page);

Same as the follow feature, if you try to block, unblock or checking if the model is blocking another model will always return `false` if the `CanBeBlocked` and `Blockable` are not implemented correctly.

# Liking
For models that can like other models:
```php
use Rennokki\Befriended\Traits\CanLike;
use Rennokki\Befriended\Contracts\Liker;

class User extends Model implements Liker {
use CanLike;
...
}
```

For models that can be liked:
```php
use Rennokki\Befriended\Traits\CanBeLiked;
use Rennokki\Befriended\Contracts\Likeable;

class Page extends Model implements Likeable {
use CanBeLiked;
...
}
```

For both liking & being liked:
```php
use Rennokki\Befriended\Traits\Like;
use Rennokki\Befriended\Contracts\Liking;

class User extends Model implements Liking {
use Like;
...
}
```

You can use the following methods:
```php
$user->like($page);
$user->unlike($page);

$user->likings(); // Users that this user likes.
$user->likings(Page::class); // Pages that this user likes.
$user->likers(); // Users that like this user.
$user->likers(Page::class); // Pages that like this user.

$user->isLiking($page);
```

# Filtering content
Filtering content is what this packages makes it happen to be BE-AU-TIFUL. When querying for your results, you can use the `CanFilterFollowingModels` and `CanFilterBlockedModels` scopes.

Expand Down
4 changes: 3 additions & 1 deletion src/BefriendedServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function boot()
], 'config');

$this->publishes([
__DIR__.'/../database/migrations/2018_07_14_183253_befriended.php' => database_path('migrations/2018_07_14_183253_befriended.php'),
__DIR__.'/../database/migrations/2018_07_14_183253_followers.php' => database_path('migrations/2018_07_14_183253_followers.php'),
__DIR__.'/../database/migrations/2018_07_14_183254_blockers.php' => database_path('migrations/2018_07_14_183254_blockers.php'),
__DIR__.'/../database/migrations/2018_07_14_183255_likers.php' => database_path('migrations/2018_07_14_183255_likers.php'),
], 'migration');
}

Expand Down
8 changes: 8 additions & 0 deletions src/Contracts/Likeable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rennokki\Befriended\Contracts;

interface Likeable
{
public function likers($model = null);
}
14 changes: 14 additions & 0 deletions src/Contracts/Liker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rennokki\Befriended\Contracts;

interface Liker
{
public function liking($model = null);

public function isLiking($model);

public function like($model);

public function unlike($model);
}
8 changes: 8 additions & 0 deletions src/Contracts/Liking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rennokki\Befriended\Contracts;

interface Liking extends Likeable, Liker
{
//
}
24 changes: 24 additions & 0 deletions src/Models/LikerModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rennokki\Befriended\Models;

use Illuminate\Database\Eloquent\Model;

class LikerModel extends Model
{
protected $table = 'likers';
protected $fillable = [
'likeable_id', 'likeable_type',
'liker_id', 'liker_type',
];

public function likeable()
{
return $this->morphTo();
}

public function liker()
{
return $this->morphTo();
}
}
14 changes: 14 additions & 0 deletions src/Traits/CanBeLiked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rennokki\Befriended\Traits;

trait CanBeLiked
{
public function likers($model = null)
{
return $this->morphToMany(($model) ?: $this->getMorphClass(), 'likeable', 'likers', 'likeable_id', 'liker_id')
->withPivot('liker_type')
->wherePivot('liker_type', ($model) ?: $this->getMorphClass())
->wherePivot('likeable_type', $this->getMorphClass());
}
}
58 changes: 58 additions & 0 deletions src/Traits/CanLike.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Rennokki\Befriended\Traits;

use Rennokki\Befriended\Contracts\Liker;
use Rennokki\Befriended\Contracts\Liking;

trait CanLike
{
public function liking($model = null)
{
return $this->morphToMany(($model) ?: $this->getMorphClass(), 'liker', 'likers', 'liker_id', 'likeable_id')
->withPivot('likeable_type')
->wherePivot('likeable_type', ($model) ?: $this->getMorphClass())
->wherePivot('liker_type', $this->getMorphClass());
}

public function isLiking($model)
{
if (! $model instanceof Liker && ! $model instanceof Liking) {
return false;
}

return (bool) ! is_null($this->liking($model->getMorphClass())->find($model->getKey()));
}

public function like($model)
{
if (! $model instanceof Liker && ! $model instanceof Liking) {
return false;
}

if ($this->isLiking($model)) {
return false;
}

$this->liking()->attach($this->getKey(), [
'liker_id' => $this->getKey(),
'likeable_type' => $model->getMorphClass(),
'likeable_id' => $model->getKey(),
]);

return true;
}

public function unlike($model)
{
if (! $model instanceof Liker && ! $model instanceof Liking) {
return false;
}

if (! $this->isLiking($model)) {
return false;
}

return (bool) $this->liking($model->getMorphClass())->detach($model->getKey());
}
}
8 changes: 8 additions & 0 deletions src/Traits/Like.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rennokki\Befriended\Traits;

trait Like
{
use CanLike, CanBeLiked;
}
Loading

0 comments on commit 71ae278

Please sign in to comment.