Skip to content

Commit 59089d5

Browse files
committed
Making follows polymorphic. resolved #170
1 parent 7e126f7 commit 59089d5

17 files changed

+524
-355
lines changed

.travis.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

README.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,43 @@ php artisan vendor:publish
3636

3737
### Traits
3838

39-
#### `Overtrue\LaravelFollow\Followable`
39+
#### `Overtrue\LaravelFollow\Traits\Follower`
40+
41+
Add the Follower trait to your user model:
4042

4143
```php
4244

43-
use Illuminate\Notifications\Notifiable;
44-
use Illuminate\Contracts\Auth\MustVerifyEmail;
45-
use Illuminate\Foundation\Auth\User as Authenticatable;
45+
use Overtrue\LaravelFavorite\Traits\Favoriter;
46+
47+
class User extends Authenticatable
48+
{
49+
use Follower;
50+
51+
<...>
52+
}
53+
```
54+
55+
#### `Overtrue\LaravelFollow\Followable`
56+
57+
Then add the Followable trait to your followable model, for example `App\User`:
58+
59+
```php
4660
use Overtrue\LaravelFollow\Followable;
4761

4862
class User extends Authenticatable
4963
{
64+
use Followable;
5065
<...>
66+
}
67+
```
68+
69+
or any other model:
70+
71+
```php
72+
use Overtrue\LaravelFollow\Followable;
73+
74+
class Channel extends Model
75+
{
5176
use Followable;
5277
<...>
5378
}
@@ -68,8 +93,6 @@ $user1->rejectFollowRequestFrom($user2);
6893
$user1->isFollowing($user2);
6994
$user2->isFollowedBy($user1);
7095
$user2->hasRequestedToFollow($user1);
71-
72-
$user1->areFollowingEachOther($user2);
7396
```
7497

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

122145
```php
123-
$users = User::withCount(['followings', 'followers'])->get();
146+
$users = User::withCount(['followings', 'followables'])->get();
124147
// or
125148
$users = User::withCount(['approvedFollowings', 'approvedFollowers'])->get();
126149

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

136159
### Attach user follow status to followable collection
137160

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

140163
#### For model
141164

@@ -238,7 +261,7 @@ foreach($users as $user) {
238261
$user->isFollowing(2);
239262
}
240263

241-
$users = User::with('followers')->get();
264+
$users = User::with('followables')->get();
242265

243266
foreach($users as $user) {
244267
$user->isFollowedBy(2);

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
}
2323
},
2424
"require-dev": {
25-
"mockery/mockery": "^1.4",
25+
"mockery/mockery": "^1.5",
2626
"phpunit/phpunit": "^9.5",
27-
"orchestra/testbench": "^7.0",
28-
"friendsofphp/php-cs-fixer": "^3.0"
27+
"orchestra/testbench": "^7.4",
28+
"friendsofphp/php-cs-fixer": "^3.8"
2929
},
3030
"extra": {
3131
"laravel": {

config/follow.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22

33
return [
44
/*
5-
* Table name for followers records.
5+
* Use uuid as primary key.
66
*/
7-
'relation_table' => 'user_follower',
7+
'uuids' => false,
8+
9+
/*
10+
* User tables foreign key name.
11+
*/
12+
'user_foreign_key' => 'user_id',
13+
14+
/*
15+
* Table name for followers table.
16+
*/
17+
'followables_table' => 'followables',
18+
19+
/**
20+
* Model class name for followers table.
21+
*/
22+
'followables_model' => \Overtrue\LaravelFollow\Followable::class,
823
];

migrations/2020_04_04_000000_create_user_follower_table.php

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateFollowablesTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create(config('follow.followables_table', 'followables'), function (Blueprint $table) {
12+
$table->id();
13+
$table->unsignedBigInteger(config('follow.user_foreign_key', 'user_id'))->index()->comment('user_id');
14+
if (config('follow.uuids')) {
15+
$table->uuidMorphs('followable');
16+
} else {
17+
$table->morphs('followable');
18+
}
19+
20+
$table->timestamp('accepted_at')->nullable();
21+
$table->timestamps();
22+
23+
$table->index(['followable_type', 'accepted_at']);
24+
});
25+
}
26+
27+
public function down()
28+
{
29+
Schema::dropIfExists(config('follow.followables_table', 'followables'));
30+
}
31+
}

phpunit.xml.dist

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false">
11-
<testsuites>
12-
<testsuite name="Application Test Suite">
13-
<directory>./tests/</directory>
14-
</testsuite>
15-
</testsuites>
16-
<filter>
17-
<whitelist>
18-
<directory suffix=".php">src/</directory>
19-
</whitelist>
20-
</filter>
2+
<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">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Application Test Suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
2113
</phpunit>

src/Events/Event.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
22

33
namespace Overtrue\LaravelFollow\Events;
44

5-
use Overtrue\LaravelFollow\UserFollower;
5+
use Overtrue\LaravelFollow\Followable;
66

77
class Event
88
{
9-
/**
10-
* @var int|string
11-
*/
12-
public $followingId;
9+
public int|string $followable_id;
10+
public int|string $followable_type;
11+
public int|string $follower_id;
12+
public int|string $user_id;
1313

14-
/**
15-
* @var int|string
16-
*/
17-
public $followerId;
14+
protected Followable $relation;
1815

19-
protected UserFollower $relation;
20-
21-
public function __construct(UserFollower $relation)
16+
public function __construct(Followable $follower)
2217
{
23-
$this->relation = $relation;
24-
$this->followerId = $relation->follower_id;
25-
$this->followingId = $relation->following_id;
18+
$this->follower_id = $this->user_id = $follower->{\config('follow.user_foreign_key', 'user_id')};
19+
$this->followable_id = $follower->followable_id;
20+
$this->followable_type = $follower->followable_type;
2621
}
2722
}

0 commit comments

Comments
 (0)