Skip to content

Commit

Permalink
Fixed #70 #76
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Dec 2, 2018
1 parent b760c72 commit d0239c5
Show file tree
Hide file tree
Showing 23 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Events/Event.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Events/RelationAttached.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Events/RelationAttaching.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Events/RelationDetached.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Events/RelationDetaching.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Events/RelationToggled.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Events/RelationToggling.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
10 changes: 3 additions & 7 deletions src/Follow.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down Expand Up @@ -74,15 +74,11 @@ public static function isRelationExists(Model $model, $relation, $target, $class
{
$target = self::formatTargets($target, $class ?: config('follow.user_model'));

$relationKey = $class ? 'followable_id' : config('follow.users_table_foreign_key', 'user_id');

$relationKey = self::tablePrefixedField($relationKey);

if ($model->relationLoaded($relation)) {
return $model->{$relation}->where($relationKey, head($target->ids))->isNotEmpty();
return $model->{$relation}->where('id', head($target->ids))->isNotEmpty();
}

return $model->{$relation}($target->classname)->where($relationKey, head($target->ids))->exists();
return $model->{$relation}($target->classname)->where('id', head($target->ids))->exists();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/FollowRelation.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/FollowServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanBeBookmarked.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanBeFavorited.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanBeFollowed.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanBeLiked.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanBeSubscribed.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanBeVoted.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanBookmark.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanFavorite.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanFollow.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanLike.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanSubscribe.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanVote.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the overtrue/laravel-follow
* This file is part of the overtrue/laravel-follow.
*
* (c) overtrue <i@overtrue.me>
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Traits/CanFollowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,20 @@ public function test_following_each_other()
$user2->follow($user1);
$this->assertTrue($user1->areFollowingEachOther($user2));
}

public function test_eager_loading()
{
$user1 = User::find(1);
$user2 = User::find(2);

$user1->follow($user2);
$user2->follow($user1);

// eager loading
$user2 = User::find(2)->load(['followings', 'followers']);
$this->assertTrue($user2->isFollowedBy($user1));

// without eager loading
$this->assertTrue($user1->isFollowedBy($user2));
}
}

0 comments on commit d0239c5

Please sign in to comment.