Skip to content

Commit

Permalink
Making follows polymorphic. resolved #170
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed May 2, 2022
1 parent 7e126f7 commit 59089d5
Show file tree
Hide file tree
Showing 17 changed files with 524 additions and 355 deletions.
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,43 @@ php artisan vendor:publish

### Traits

#### `Overtrue\LaravelFollow\Followable`
#### `Overtrue\LaravelFollow\Traits\Follower`

Add the Follower trait to your user model:

```php

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Overtrue\LaravelFavorite\Traits\Favoriter;

class User extends Authenticatable
{
use Follower;

<...>
}
```

#### `Overtrue\LaravelFollow\Followable`

Then add the Followable trait to your followable model, for example `App\User`:

```php
use Overtrue\LaravelFollow\Followable;

class User extends Authenticatable
{
use Followable;
<...>
}
```

or any other model:

```php
use Overtrue\LaravelFollow\Followable;

class Channel extends Model
{
use Followable;
<...>
}
Expand All @@ -68,8 +93,6 @@ $user1->rejectFollowRequestFrom($user2);
$user1->isFollowing($user2);
$user2->isFollowedBy($user1);
$user2->hasRequestedToFollow($user1);

$user1->areFollowingEachOther($user2);
```

#### Get followings:
Expand Down Expand Up @@ -120,7 +143,7 @@ $user->notApprovedFollowers()->count();
List with `*_count` attribute:

```php
$users = User::withCount(['followings', 'followers'])->get();
$users = User::withCount(['followings', 'followables'])->get();
// or
$users = User::withCount(['approvedFollowings', 'approvedFollowers'])->get();

Expand All @@ -135,7 +158,7 @@ foreach($users as $user) {

### Attach user follow status to followable collection

You can use `Followable::attachFollowStatus(Collection $followables)` to attach the user favorite status, it will set `has_followed` attribute to each model of `$followables`:
You can use `Follower::attachFollowStatus(Collection $followables)` to attach the user favorite status, it will set `has_followed` attribute to each model of `$followables`:

#### For model

Expand Down Expand Up @@ -238,7 +261,7 @@ foreach($users as $user) {
$user->isFollowing(2);
}

$users = User::with('followers')->get();
$users = User::with('followables')->get();

foreach($users as $user) {
$user->isFollowedBy(2);
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
}
},
"require-dev": {
"mockery/mockery": "^1.4",
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.5",
"orchestra/testbench": "^7.0",
"friendsofphp/php-cs-fixer": "^3.0"
"orchestra/testbench": "^7.4",
"friendsofphp/php-cs-fixer": "^3.8"
},
"extra": {
"laravel": {
Expand Down
19 changes: 17 additions & 2 deletions config/follow.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@

return [
/*
* Table name for followers records.
* Use uuid as primary key.
*/
'relation_table' => 'user_follower',
'uuids' => false,

/*
* User tables foreign key name.
*/
'user_foreign_key' => 'user_id',

/*
* Table name for followers table.
*/
'followables_table' => 'followables',

/**
* Model class name for followers table.
*/
'followables_model' => \Overtrue\LaravelFollow\Followable::class,
];
24 changes: 0 additions & 24 deletions migrations/2020_04_04_000000_create_user_follower_table.php

This file was deleted.

31 changes: 31 additions & 0 deletions migrations/2022_05_02_000000_create_followables_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

class CreateFollowablesTable extends Migration
{
public function up()
{
Schema::create(config('follow.followables_table', 'followables'), function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger(config('follow.user_foreign_key', 'user_id'))->index()->comment('user_id');
if (config('follow.uuids')) {
$table->uuidMorphs('followable');
} else {
$table->morphs('followable');
}

$table->timestamp('accepted_at')->nullable();
$table->timestamps();

$table->index(['followable_type', 'accepted_at']);
});
}

public function down()
{
Schema::dropIfExists(config('follow.followables_table', 'followables'));
}
}
30 changes: 11 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
<?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="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
25 changes: 10 additions & 15 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@

namespace Overtrue\LaravelFollow\Events;

use Overtrue\LaravelFollow\UserFollower;
use Overtrue\LaravelFollow\Followable;

class Event
{
/**
* @var int|string
*/
public $followingId;
public int|string $followable_id;
public int|string $followable_type;
public int|string $follower_id;
public int|string $user_id;

/**
* @var int|string
*/
public $followerId;
protected Followable $relation;

protected UserFollower $relation;

public function __construct(UserFollower $relation)
public function __construct(Followable $follower)
{
$this->relation = $relation;
$this->followerId = $relation->follower_id;
$this->followingId = $relation->following_id;
$this->follower_id = $this->user_id = $follower->{\config('follow.user_foreign_key', 'user_id')};
$this->followable_id = $follower->followable_id;
$this->followable_type = $follower->followable_type;
}
}
Loading

0 comments on commit 59089d5

Please sign in to comment.