🇨🇳 Shenzhen,China・🕹Web developer
如果你喜欢我的项目并想支持我,点击这里 ❤️
:heart: This package helps you to add user based follow system to your model.
License: MIT License
🇨🇳 Shenzhen,China・🕹Web developer
如果你喜欢我的项目并想支持我,点击这里 ❤️
I like this package but what i wonder is why you keep all the data in one table when you search for user likes for example mysql should find followable_type first from all the followable types in the table and it can maybe increase query time ?
Great package you saved me a bunch of time from needed to build something like this. I just upgraded to 1.1.5 and realized my unit tests are failing.
It appears to be an issue with relationship mapping for the relationship type 'likes'.
Here is the output of my tests.
Exception: Invalid relation definition.
/xxx/vendor/overtrue/laravel-follow/src/Follow.php:178 /xxx/vendor/overtrue/laravel-follow/src/Follow.php:132 /xxx/vendor/overtrue/laravel-follow/src/Follow.php:115 /xxx/vendor/overtrue/laravel-follow/src/Traits/CanLike.php:61
you have the following set but likes aren't included
const RELATION_LIKE = 'like';
const RELATION_FOLLOW = 'follow';
const RELATION_SUBSCRIBE = 'subscribe';
const RELATION_FAVORITE = 'favorite';
const RELATION_UPVOTE = 'upvote';
const RELATION_DOWNVOTE = 'downvote';
/**
* @var array
*/
protected static $relationMap = [
'followings' => 'follow',
'followers' => 'follow',
'favoriters' => 'favorite',
'favorites' => 'favorite',
'subscriptions' => 'subscribe',
'subscribers' => 'subscribe',
'upvotes' => 'upvote',
'upvoters' => 'upvote',
'downvotes' => 'downvote',
'downvoters' => 'downvote',
];
Hi
I cant seem to get it to show many many likers on a post.
in my blade view I have been using
{{ $post->likers()->get()->count() }}
It will just show zero everytime.
I can see in the database it is saving the like but I cannot return the total number of likes for post.
Can somebody help please.
hello
When executing the publish command, it only writes:
Publishing complete
But the files are not added
sorry! no megeraton
$user->followings();
$object->followers();
这样获取到的是什么对象? 是否需要用foreach遍历? 直接获取用户ID啥的不行
另外怎样统计用户关注/被关注的用户数量?
Since moving from 1.1.7 to 1.1.8 I now get
Incorrect syntax near the keyword 'IS'. (SQL: select [users].*, pivot_followables.user_id IS NOT NULL AS pivot_each_other, [followables].[followable_id] as [pivot_followable_id], [followables].[user_id] as [pivot_user_id], [followables].[followable_type] as [pivot_followable_type], [followables].[relation] as [pivot_relation], [followables].[created_at] as [pivot_created_at] from [users] inner join [followables] on [users].[id] = [followables].[user_id] left join [followables] as [pivot_followables] on [pivot_followables].[followable_type] = 'App\Suggestion' and [pivot_followables].[followable_id] = [followables].[user_id] and [pivot_followables].[user_id] = [followables].[followable_id] where [followables].[followable_id] = 1066 and [followables].[followable_type] = App\Suggestion and [followables].[relation] = follow
when going $object->followers()->get()
everything was working fine with Laravel 5.2, But after migrating to 5.3 only follow
and favorite
relations are working, when I use like
relation I got this error:
Exception: Invalid relation definition. in /var/www/html/vitee_5.3/vendor/overtrue/laravel-follow/src/Follow.php:175 Stack trace: #0 /var/www/html/vitee_5.3/vendor/overtrue/laravel-follow/src/Follow.php(129): Overtrue\LaravelFollow\Follow::getRelationTypeFromRelation(Object(Illuminate\Database\Eloquent\Relations\MorphToMany)) #1 /var/www/html/vitee_5.3/vendor/overtrue/laravel-follow/src/Follow.php(80): Overtrue\LaravelFollow\Follow::attachPivotsFromRelation(Object(Illuminate\Database\Eloquent\Relations\MorphToMany), Object(App\Models\Event), 'App\\Models\\Clie...') #2 /var/www/html/vitee_5.3/vendor/overtrue/laravel-follow/src/Traits/CanLike.php(33): Overtrue\LaravelFollow\Follow::attachRelations(Object(App\Models\Client), 'likes', Object(App\Models\Event), 'App\\Models\\Clie...') #3 /var/www/html/vitee_5.3/app/Http/Controllers/ClientController.php(296): App\Models\Client->like(Object(App\Models\Event)) #4 [internal function]: App\Http\Controllers\ClientController->likeEvent('3') #5 /var/www/html/vitee_5.3/bootstrap/cache/compiled.php(9584): call_user_func_array(Array, Array) #6 /var/www/html/vitee_5.3/bootstrap/cache/compiled.php(9611): Illuminate\Routing\Controller->callAction('likeEvent', Array) #7 /var/www/html/vitee_5.3/bootstrap/cache/compiled.php(8665): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\ClientController), 'likeEvent') #8 /var/www/html/vitee_5.3/bootstrap/cache/compiled.php(8646): Illuminate\Routing\Route->runController() #9 /var/www/html/vitee_5.3/bootstrap/cache/compiled.php(8355): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request)) #10 /var/www/html/vitee_5.3/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #11 /var/www/html/vitee_5.3/app/Http/Middleware/Authenticate.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #12 /var/www/html/vitee_5.3/bootstrap/cache/compiled.php(10069): App\Http\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'client') #13 /var/www/html/vitee_5.3/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #14 /var/www/html/vitee_5.3/bootstrap/cache/compiled.php(10177): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #15 /var/www/html/vitee_5.3/bootstrap/cache/compiled.php(10069): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure)) #16 /var/www/html/vitee_5.3/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
Good
Hey there.
I honestly love this package, I use it for my platform and it is working so well it's really impressive, well done!
However, this is not scaled for larger scale applications, I am using it for my platform with around 25000 users, we have a dashboard where we have a facebook like timeline, here is a screenshot:
https://i.imgur.com/r4LGdlx.png
The dashboard is the first thing that loads in, it loads in the latest 10 posts and displays the post one by one, for each post you can like it and see if you liked it, the same goes for comments on status as you can see here:
https://i.imgur.com/RnwyVsY.png
Now the issue we are seeing with this package is with the functions:
hasLiked()
getLikers()
The returns from there functions is not cached using Laravel Cache, therefor everytime a user loads in the dashboard it will query the database using the above functions (hasLiked, getLikers) on each post and each comment.
You may wonder what is the bad about this, well imagine we have 10 posts, each post has 10 comments.
When you load in the dashboard it would run hasLiked on each post and each comment, that means it queries the database 100 times everytime you load the dashboard.
Then for each comment and status it will also get all the likers, what happens now is that it queries the database again 100 times and for each comment and since we want to also receive the avatar, username and id of each commenter it will also query the user table an additional time for each comment.
I hope this makes sense.
How can you resolve this issue?
https://laravel.com/docs/5.4/cache
For example whenever like() or unlike() gets called, you could either set a key that is something like this when you like something:
{UserID}:hasLiked:{ModelName}:{ModelID}
And then remove it when ->unlike is called
Then you just see if the key exists using ->hasLiked
Could you please look into if this is possible, I am currently looking into implementing it myself in the user trait like so:
https://i.imgur.com/gv4wHag.png
And myself remove or set the key in controllers.
Thank you for this amazing plugin, feel free to use us as an example of use: https://altpocket.io (We use the following part for each user aswell! =))
1、便是找不到CanBeLike之类的。
2、在使用 $post->likers()时 withPovit 又没有被定义。
挺喜欢这个插件的,希望作者能修正。
Hi, I found a bug in Traits\CanBeFollowed.php
that results in an error table users not found
since I don't use users
table as my default.
$userTable = config('follow.user_table', 'users');
should be
$userTable = config('follow.users_table_name', 'users');
hello, overtrue...
if I delete post or user and how to delete all vote and follow records from a database.
just got that when importing DB any inside ?
Error
SQL query:
CREATE TABLE `followables` (
`user_id` INT( 10 ) UNSIGNED NOT NULL ,
`followable_id` INT( 10 ) UNSIGNED NOT NULL ,
`followable_type` VARCHAR( 191 ) COLLATE utf8mb4_unicode_ci NOT NULL ,
`relation` VARCHAR( 191 ) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'follow' COMMENT 'follow/like/subscribe/favorite/upvote/downvote',
`deleted_at` TIMESTAMP NULL DEFAULT NULL ,
`created_at` TIMESTAMP NULL DEFAULT NULL ,
`updated_at` TIMESTAMP NULL DEFAULT NULL ,
KEY `followables_user_id_foreign` ( `user_id` ) ,
KEY `followables_followable_type_index` ( `followable_type` ) ,
CONSTRAINT `followables_user_id_foreign` FOREIGN KEY ( `user_id` ) REFERENCES `users` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
MySQL said: Documentation
#1215 - Cannot add foreign key constraint
Hello,
What do you think about soft delete implementation on detach way?
If it's possible, could I send a PR to you? Because I want to know how much users are subscribed and unsubscribed.
Hi,
I am working on a project and wanted to check if this package can be used with laravel-tagging? So every post has tags.
What I want?
Users to like/follow tags and will see posts based on tags they liked, followed, etc.
Just wanted to check if I can use the two packages together to achieve my requirement.
I'm really excited about this package. I published the config file to be able to extend and use the BelongsToTenant trait on the model but couldn't get it to work. In other words I have company users and would like to add the company id to the followables table. Would you please help me figure out the easiest way to do that with this package. Thank you.
Hello, Thank you for the great package. I have been using it lately and I thought a feature allowing to select the time period of the different actions would be nice. This package uses a custom class based on Carbon. I thought it might be of help.
Thank you again
php artisan migrate
----- Nothing to migrate.
????
单元测试中使用了Illuminate\Filesystem\ClassFinder
,但是在laravel5.4却已经移除了,可以我在laravel5.2中也没跑成功,显示No tests executed!
。
I have Users, Clients, and Organisers.
Client canFollow Organiser. However when I tried:
$client = App\Models\Client::find(1);
$organiser = App\Models\Organiser::find(1);
$client->follow(1, 'App\Models\Organiser');
I got this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'client_id' in 'where clause' (SQL: select
followable_id
fromfollowables
whererelation
= follow andclient_id
= 1 andfollowable_type
= App\Models\Organiser)
I'm using just id
in my tables with no prefix (e.g $client->id
), but your package somehow uses client_id
as primary key.
I tried to edit the config/follow.php
file to use App\Models\Client
as the user model and id
as the primary key but still got the same error.
There is a minor typo error in the migration file. Please find attached a PR that fixes the error #46
Thanks in advance
use Overtrue\LaravelFollow\Traits\CanBeLiked;
use Overtrue\LaravelFollow\Traits\CanBeFavorited;
use Overtrue\LaravelFollow\Traits\CanBeVoted;
use Overtrue\LaravelFollow\Traits*Can(Be)Bookmarked*;
class Post extends Model
{
use CanBeLiked, CanBeFavorited, CanBeVoted, CanBeBookmarked;
}
Hi, I'm trying to get data with the getTargetsCollection() function in the event and I'm not making it.
This is the error:
[2018-09-17 20:53:23] testing.ERROR: SQLSTATE[HY000]: General error: 25 column index out of range (SQL: select * from "posts" where "posts"."id" in (App\Models\Post, 1, upvote) and "posts"."deleted_at" is null) {"userId":1,"email":"[email protected]","exception":"[object] (Illuminate\\Database\\QueryException(code: HY000): SQLSTATE[HY000]: General error: 25 column index out of range (SQL: select * from \"posts\" where \"posts\".\"id\" in (App\\Models\\Post, 1, upvote) and \"posts\".\"deleted_at\" is null) at C:\\xampp\\htdocs\\Coderos\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:664, Doctrine\\DBAL\\Driver\\PDOException(code: HY000): SQLSTATE[HY000]: General error: 25 column index out of range at C:\\xampp\\htdocs\\Coderos\\vendor\\doctrine\\dbal\\lib\\Doctrine\\DBAL\\Driver\\PDOStatement.php:144, PDOException(code: HY000): SQLSTATE[HY000]: General error: 25 column index out of range at C:\\xampp\\htdocs\\Coderos\\vendor\\doctrine\\dbal\\lib\\Doctrine\\DBAL\\Driver\\PDOStatement.php:142)
如题,A关注B B关注A。有没有 判断他们是不是互相关注的方法呢
After php artisan vendor:publish
I migrated the tables.
My User Model looks like this:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Overtrue\LaravelFollow\Traits\CanBeFollowed;
use Overtrue\LaravelFollow\Traits\CanFollow;
class User extends Authenticatable
{
use CanFollow, CanBeFollowed;
....
}
But I am getting
FatalErrorException in User.php line 11:
Trait 'Overtrue\LaravelFollow\Traits\CanFollow' not found
that's when i try to check if a model has down/up voted a another model ( model->hasUpvoted() )
This is a wonderful addon that I have been using for my social portfolio website, it works wonders.
However, right now I am using Debugbar to see what queries takes long time, and it seems like laravel-follow is using a lot on our dashboard where we have a social feed like facebook.
Is there any simple way to cache laravel followable?
如题,如何按照最新的收藏排在最前面?
Hi,
Thank you for your innovative package.
I am wondering it would be great if it would support Upvote and Downvote as well.
Of course it might be possible to develop it with traits, but I believe it's great to include it in the package. After all, it already has four different states.
I updated the package today, and after that, I received an SQL error when using hasLiked
.
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
I'm using the hasLiked
as it's used in the README, $user->hasLiked($target)
I downgraded the package to 1.1.10, and everything works again
Like the title, how would you go to get the top followed user?
Nice package, using it for my site right now really great work man.
To avoid recreating migrations. Nice library btw
if (!class_exists('CreateLaravelFollowTables')) {
}
When a Auth user (id: 1) follows user with id:2
But when the same Auth user follows user with id:3, the column is replaced with
Also nothing is added in the followers table, it always remains empty!!
This is my code:
public function userFollow($id)
{
$authuser = Auth::user();
$user = User::find($id);
$authuser->follow($user);
return redirect()->back()->with('message', 'Following User');
}
public function userUnfollow($id)
{
$authuser = Auth::user();
$user = User::find($id);
$authuser->unfollow($user);
return redirect()->back()->with('message', 'Unfollowing User');
}
Am I doing something wrong?
Version: 1.1.10
dd(\App\Review::find(1)->isVotedBy(1)); //true
Which is looking for: [Follow::RELATION_UPVOTE, Follow::RELATION_DOWNVOTE]
dd(\App\Review::find(1)->isUpvotedBy(1)); //false
(should be true)
Which is looking for: Follow::RELATION_UPVOTE
dd(\App\Review::find(1)->isDownvotedBy(1)); //false
Which is looking for: Follow::RELATION_DOWNVOTE
Any idea why is that?
现在有需求需要在访问产品详情的时候去查询判断是否收藏产品,关注店铺。希望在调用方法的时候能从缓存去读取数据,在关注收藏的时候写入或更新缓存!!
Hi, upon continued analysis, it seems you have manually written user_id in quite a few places.
I just stumbled upon one:
$profile->isFollowedBy(Auth::user())
This throws an error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause' (SQL: select exists(select * from profiles
inner join profile_followers
on profiles
.id
= profile_followers
.profile_id
where profile_followers
.followable_id
= 1 and profile_followers
.followable_type
= App\Profile and profile_followers
.relation
= follow and user_id
= 1) as exists
)
public static function isRelationExists(Model $model, $relation, $target, $class = null)
{
$target = self::formatTargets($target, $class ?: config('follow.user_model'));
return $model->{$relation}($target->classname)
->where($class ? 'followable_id' : 'user_id', head($target->ids))->exists();
}
Could you update the user_id to the config value.
Just fyi I will try to download the code and help you with this, but if you can fix this particular one would be a great help thanks.
I am looking to make something like Instagram where user can make their profile private and when someone follows a user, they would have to approve it.
I have added a new column is_private
to the users
table and is_approved
to the followables
table.
Currently I am getting a user followers with this:
$allfollowers = $user->followers()->get();
What should I do to check for is_approved
field?
Any suggestion will be really appreciated.
Thank you.
Could we have a ->count() function?
Ex: $user->followers()->count()
That perhaps is cached, right now this is how it looks like in debugbar:
https://i.imgur.com/vvI5KPu.png
Just to count a users followers.
my code
$user=Users::find($request->user()->id);
dd($user->followings()->get());
but it made a exception
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'waijie_followables.users_id' in 'field list' (SQL: select `waijie_users`.*, (CASE WHEN `waijie_pivot_followables`.`user_id` IS NOT NULL THEN 1 ELSE 0 END) as `pivot_each_other`, `waijie_followables`.`users_id` as `pivot_users_id`, `waijie_followables`.`followable_id` as `pivot_followable_id`, `waijie_followables`.`followable_type` as `pivot_followable_type`, `waijie_followables`.`relation` as `pivot_relation`, `waijie_followables`.`created_at` as `pivot_created_at` from `waijie_users` inner join `waijie_followables` on `waijie_users`.`id` = `waijie_followables`.`followable_id` left join `waijie_followables` as `waijie_pivot_followables` on `waijie_pivot_followables`.`followable_type` = 'App\\\\Model\\\\Users' and `waijie_pivot_followables`.`followable_id` = `waijie_followables`.`user_id` and `waijie_pivot_followables`.`user_id` = `waijie_followables`.`followable_id` where `waijie_followables`.`users_id` = 1 and `waijie_followables`.`followable_type` = App\\Model\\Users and `waijie_followables`.`relation` = follow and `waijie_users`.`deleted_at` is null)
I have tried the following but it doesn't work
`
public function followProfessional($slug)
{
$professional = Professional::where('slug', $slug)->first();
if(! $professional) {
return redirect()->back()->with('error', 'Professional does not exist.');
}
$user = Auth::user()->id;
$user->follow($professional);
return redirect()->back()->with('success', 'You are currently following this professional.');
}
`
And in my view
<a href="{{ url('professionals/'.$professional->category->slug.'/'.$professional->slug.'/follow') }}" class="btn btn-sm white">Follow</a>
And in my route
Route::get('professionals/{professionalcategory}/{slug}/follow', 'ProfessionalController@followProfessional')->name('professional.follow');
When i click on the follow button the page just refreshes back but doesn't add anything to the followables table
Could anyone help me out in this?
Hi.
I wonder if any can help.
Each user can Upvote and Post (Post Model).
I want to be able to count the number of upvote a user has received. for example
User has 5 Posts and each post has received 2 Upvotes.
Total upvotes = 10.
I have tried
$user->posts->upvoters()->get()->count()
But this gives me upvoters is not on this collection.
Thanks in advance
Example:
$user->favorites()->orderBy('id', 'desc')->get();
This program will result a sorted collection by the favorites item's ID. Mostly, We need to sorted with the action time.
It seems that only the User model is supported to use the canXXX traits. For example if i have a Student model even when i give it the "canLike" trait and say $student->likes(1,'App\Subject') it throws an error stating that "can't find student_id in the followables table"
I am loading a list of items that has the CanBeFavorited
trait. I am also loading the authed users favorites with auth()->user()->load('favorites')
. However, the hasFavorited($item)
is not using the already loaded relationship, but instead it is querying the database creating an n+1 problem.
A possible solution would be to check in the hasFavorited
if the relation is already loaded, using relationLoaded('favorites')
, then use that.
Hi, I am trying to implement this package, with the following code...
$profile = Profile::find($profile->id);
$userProfile = Profile::find(Auth::user()->profile_id);
$userProfile->follow($profile, 'App\Profile');
I have updated the tables to use profile_id instead of user_id. I am not sure if this is causing the issue, but am getting the following error:
Exception
Invalid relation definition.
protected static function getRelationTypeFromRelation(MorphToMany $relation)
{
$wheres = array_pluck($relation->getQuery()->getQuery()->wheres, 'value', 'column');
if (empty($wheres['followables.relation'])) {
throw new \Exception('Invalid relation definition.');
}
return $wheres['followables.relation'];
}
使用时不用新建一个表来存储关注吗?
Hi,
Is there a way to get the date when the relationship was created?
I got some issue
my laravel and package are latest version
when I use php artisan vendor:publish
and choosing 0(all published)
found CreateLaravelFollowTables exists two, and I found:
https://github.com/overtrue/laravel-follow/blob/master/src/FollowServiceProvider.php#L31
!class_exists('CreateLaravelFollowTables')
Not correctly working
if class exists should be return true and reverse boolean, so it should be false
, but class_exists('CreateLaravelFollowTables') always return false
and then reverse, so it always got true
and always into condition to generate migration.
I have been try sometimes, but I no have any idea what's happen, anyone?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.