• Stars
    star
    759
  • Rank 57,708 (Top 2 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.

Laravel Befriended

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models

🤝 Supporting

If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦

🚀 Installation

Install the package:

$ composer require rennokki/befriended

Publish the config:

$ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="config"

Publish the migrations:

$ php artisan vendor:publish --provider="Rennokki\Befriended\BefriendedServiceProvider" --tag="migrations"

🙌 Usage

The power of example is better here. This package allows you simply to assign followers, blockings or likes without too much effort. What makes the package powerful is that you can filter queries using scopes out-of-the-box.

$alice = User::where('name', 'Alice')->first();
$bob = User::where('name', 'Bob')->first();
$tim = User::where('name', 'Tim')->first();

$alice->follow($bob);

$alice->following()->count(); // 1
$bob->followers()->count(); // 1

User::followedBy($alice)->get(); // Just Bob shows up
User::unfollowedBy($alice)->get(); // Tim shows up

Following

To follow other models, your model should use the CanFollow trait and Follower contract.

use Rennokki\Befriended\Traits\CanFollow;
use Rennokki\Befriended\Contracts\Follower;

class User extends Model implements Follower {
    use CanFollow;
    ...
}

The other models that can be followed should use CanBeFollowed trait and Followable contract.

use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class User extends Model implements Followable {
    use CanBeFollowed;
    ...
}

If your model can both follow & be followed, you can use Follow trait and Following contract.

use Rennokki\Befriended\Traits\Follow;
use Rennokki\Befriended\Contracts\Following;

class User extends Model implements Following {
    use Follow;
    ...
}

Let's suppose we have an User model which can follow and be followed. Within it, we can now check for followers or follow new users:

$zuck = User::where('name', 'Mark Zuckerberg')->first();
$user->follow($zuck);

$user->following()->count(); // 1
$zuck->followers()->count(); // 1

Now, let's suppose we have a Page model, than can only be followed:

use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class Page extends Model implements Followable {
    use CanBeFollowed;
    ...
}

By default, if querying following() and followers() from the User instance, the relationships will return only User instances. If you plan to retrieve other instances, such as Page, you can pass the model name or model class as an argument to the relationships:

$zuckPage = Page::where('username', 'zuck')->first();

$user->follow($zuckPage);
$user->following()->count(); // 0, because it doesn't follow any User instance
$user->following(Page::class)->count(); // 1, because it follows only Zuck's page.

On-demand, you can check if your model follows some other model:

$user->isFollowing($friend);
$user->follows($friend); // alias

Some users might want to remove followers from their list. The Followable trait comes with a revokeFollower method:

$friend->follow($user);

$user->revokeFollower($friend);

Note: Following, unfollowing or checking if following models that do not correctly implement CanBeFollowed and Followable will always return false.

Filtering followed/unfollowed models

To filter followed or unfollowed models (which can be any other model) on query, your model which you will query should use the Rennokki\Befriended\Scopes\FollowFilterable trait.

If your User model can only like other Page models, your Page model should use the trait mentioned.

$bob = User::where('username', 'john')->first();
$alice = User::where('username', 'alice')->first();

User::followedBy($bob)->get(); // You will get no results.
User::unfollowedBy($bob)->get(); // You will get Alice.

$bob->follow($alice);
User::followedBy($bob)->get(); // Only Alice pops up.

Blocking

Most of the functions are working like the follow feature, but this is helpful when your models would like to block other models.

Use CanBlock trait and Blocker contract to allow the model to block other models.

use Rennokki\Befriended\Traits\CanBlock;
use Rennokki\Befriended\Contracts\Blocker;

class User extends Model implements Blocker {
    use CanBlock;
    ...
}

Adding CanBeBlocked trait and Blockable contract sets the model able to be blocked.

use Rennokki\Befriended\Traits\CanBeBlocked;
use Rennokki\Befriended\Contracts\Blockable;

class User extends Model implements Blockable {
    use CanBeBlocked;
    ...
}

For both, you should be using Block trait & Blocking contract:

use Rennokki\Befriended\Traits\Block;
use Rennokki\Befriended\Contracts\Blocking;

class User extends Model implements Blocking {
    use Block;
    ...
}

Most of the methods are the same:

$user->block($user);
$user->block($page);
$user->unblock($user);

$user->blocking(); // Users that this user blocks.
$user->blocking(Page::class); // Pages that this user blocks.
$user->blockers(); // Users that block this user.
$user->blockers(Page::class); // Pages that block this user.

$user->isBlocking($page);
$user->blocks($page); // alias to isBlocking

Filtering blocked models

Blocking scopes provided takes away from the query the models that are blocked. Useful to stop showing content when your models blocks other models.

Make sure that the model that will be queried uses the Rennokki\Befriended\Scopes\BlockFilterable trait.

$bob = User::where('username', 'john')->first();
$alice = User::where('username', 'alice')->first();

User::withoutBlockingsOf($bob)->get(); // You will get Alice and Bob as results.

$bob->block($alice);
User::withoutBlockingsOf($bob)->get(); // You will get only Bob as result.

Liking

Apply CanLike trait and Liker contract for models that can like:

use Rennokki\Befriended\Traits\CanLike;
use Rennokki\Befriended\Contracts\Liker;

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

CanBeLiked and Likeable trait can be used for models that can be liked:

use Rennokki\Befriended\Traits\CanBeLiked;
use Rennokki\Befriended\Contracts\Likeable;

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

Planning to use both, use the Like trait and Liking contact:

use Rennokki\Befriended\Traits\Like;
use Rennokki\Befriended\Contracts\Liking;

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

As you have already got started with, these are the methods:

$user->like($user);
$user->like($page);
$user->unlike($page);

$user->liking(); // Users that this user likes.
$user->liking(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);
$user->likes($page); // alias to isLiking

Filtering liked content

Filtering liked content can make showing content easier. For example, showing in the news feed posts that weren't liked by an user can be helpful.

The model you're querying from must use the Rennokki\Befriended\Scopes\LikeFilterable trait.

Let's suppose there are 10 pages in the database.

$bob = User::where('username', 'john')->first();
$page = Page::find(1);

Page::notLikedBy($bob)->get(); // You will get 10 results.

$bob->like($page);
Page::notLikedBy($bob)->get(); // You will get only 9 results.
Page::likedBy($bob)->get(); // You will get one result, the $page

Follow requests

This is similar to the way Instagram allows you to request follow of a private profile.

To follow other models, your model should use the CanFollow trait and Follower contract.

use Rennokki\Befriended\Traits\CanFollow;
use Rennokki\Befriended\Contracts\Follower;

class User extends Model implements Follower {
    use CanFollow;
    ...
}

The other models that can be followed should use CanBeFollowed trait and Followable contract.

use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class User extends Model implements Followable {
    use CanBeFollowed;
    ...
}

If your model can both follow & be followed, you can use Follow trait and Following contract.

use Rennokki\Befriended\Traits\Follow;
use Rennokki\Befriended\Contracts\Following;

class User extends Model implements Following {
    use Follow;
    ...
}

Let's suppose we have an User model which can follow and be followed. Within it, we can now check for follower requests or request to follow a users:

$zuck = User::where('name', 'Mark Zuckerberg')->first();
$user->followRequest($zuck);

$user->followRequests()->count(); // 1
$zuck->followerRequests()->count(); // 1
$user->follows($zuck); // false
$zuck->acceptFollowRequest($user); // true
$user->follows($zuck); // true

Now, let's suppose we have a Page model, than can only be followed:

use Rennokki\Befriended\Traits\CanBeFollowed;
use Rennokki\Befriended\Contracts\Followable;

class Page extends Model implements Followable {
    use CanBeFollowed;
    ...
}

You can then request or cancel the follow requests:

$user->followRequest($zuck);
$user->cancelFollowRequest($zuck);

The one being followed can accept or decline the requests:

$zuck->acceptFollowRequest($user);
$zuck->declineFollowRequest($user);

By default, if querying followRequests() and followerRequests() from the User instance, the relationships will return only User instances.

If you plan to retrieve other instances, such as Page, you can pass the model name or model class as an argument to the relationships:

$zuckPage = Page::where('username', 'zuck')->first();

$user->followRequest($zuckPage);
$user->followRequests()->count(); // 0, because it does not have any requests from any User instance
$user->followerRequests(Page::class)->count(); // 1, because it has a follow request for Zuck's page.

Note: Requesting, accepting, declining or checking if following models that do not correctly implement CanBeFollowed and Followable will always return false.

🐛 Testing

vendor/bin/phpunit

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

🎉 Credits

More Repositories

1

laravel-eloquent-query-cache

Adding cache on your Laravel Eloquent queries' results is now a breeze.
PHP
1,014
star
2

php-k8s

Unofficial PHP client for Kubernetes. It supports any form of authentication, the exec API, and it has an easy implementation for CRDs.
PHP
296
star
3

rating

Laravel Eloquent Rating allows you to assign ratings to any model.
PHP
190
star
4

jetstream-cashier-billing-portal

Cashierstream is a simple Spark alternative written for Laravel Jetstream, with the super-power of tracking plan quotas, like seats or projects number on a per-plan basis
PHP
162
star
5

hej

Hej! is a simple authentication boilerplate for Socialite.
PHP
132
star
6

laravel-sns-events

Laravel SNS Events eases the processing of incoming SNS webhooks using Laravel Events.
PHP
131
star
7

laravel-web3

Laravel SDK wrapper for the Web3 PHP API client that interacts with the Ethereum blockchain.
PHP
112
star
8

cashier-register

Cashier Register is a simple quota feature usage tracker for Laravel Cashier subscriptions.
PHP
90
star
9

clusteer

Clusteer is a Puppeteer wrapper written for Laravel, with the super-power of parallelizing pages across multiple browser instances.
PHP
88
star
10

l1

Extend your PHP/Laravel application with Cloudflare bindings.
PHP
85
star
11

laravel-php-k8s

Just a simple port of renoki-co/php-k8s for easier access in Laravel
PHP
84
star
12

eloquent-settings

Eloquent Settings allows you to bind key-value pairs to any Laravel Eloquent model. It supports even casting for boolean, float or integer types.
PHP
80
star
13

laravel-aws-webhooks

Easy webhook handler for Laravel to catch AWS SNS notifications for various services.
PHP
69
star
14

elasticscout

ElasticScout is an optimized Laravel Scout driver for Elasticsearch 7.1+
PHP
63
star
15

laravel-helm-demo

Example of a horizontally-scaled Laravel 8 app that runs on Kubernetes with NGINX Ingress Controller.
PHP
60
star
16

laravel-healthchecks

Laravel Healthchecks is a simple controller class that helps you build your own healthchecks endpoint without issues.
PHP
55
star
17

echo-server

Echo Server is a container-ready, multi-scalable Node.js application used to host your own Socket.IO server for Laravel Broadcasting.
TypeScript
38
star
18

laravel-docker-base

Already-compiled PHP-based Images to use when deploying your Laravel application to Kubernetes using Laravel Helm charts.
31
star
19

acl

Simple, JSON-based, AWS IAM-style ACL for PHP applications, leveraging granular permissions in your applications with strong declarations. 🔐
PHP
27
star
20

octane-exporter

Export Laravel Octane metrics using this Prometheus exporter.
PHP
26
star
21

charts

Helm Charts from Renoki Co. 🚀
Smarty
26
star
22

horizon-exporter

Export Laravel Horizon metrics using this Prometheus exporter.
PHP
25
star
23

laravel-package-skeleton

Default package scaffolding for Laravel/PHP packages.
PHP
24
star
24

reddit-json-api

Reddit JSON API is a PHP wrapper for handling JSON information from public subreddits.
PHP
20
star
25

dynamodb

AWS DynamoDB Eloquent ORM for Laravel 6+
PHP
18
star
26

browser-streamer

Stream a (kiosked) website to a RMTP URL using a Firefox headless browser.
Shell
15
star
27

laravel-steampipe

Use Laravel's built-in ORM classes to query cloud resources with Steampipe.
PHP
14
star
28

thunder

Thunder is an advanced Laravel tool to track user consumption using Cashier's Metered Billing for Stripe. ⚡
PHP
13
star
29

php-helm

PHP Helm Processor is a process wrapper for Kubernetes' Helm v3 CLI. You can run programmatically Helm v3 commands, directly from PHP, with a simple syntax.
PHP
12
star
30

laravel-useful-casts

Laravel Useful Casts is a simple package for Laravel 7.0+ that comes with already-tested and already-written, useful casts for Eloquent models.
PHP
10
star
31

laravel-yaml-config

The usual Laravel .env file, but with YAML.
PHP
7
star
32

laravel-explicit-array

Improved Laravel dot notation for explicit array keys.
PHP
7
star
33

aws-elastic-client

Just a simple Elasticsearch Client handler that signs the requests for AWS Elasticsearch service with the provided credentials.
PHP
7
star
34

laravel-thermite

Laravel Thermite is an extended PostgreSQL Laravel database driver to connect to a CockroachDB cluster.
PHP
7
star
35

laravel-prerender

Prerender Laravel pages using Clusteer and this nice package.
PHP
6
star
36

laravel-acl

Simple, AWS IAM-style ACL for Laravel applications, leveraging granular permissions in your applications with strong declarations. 🔐
PHP
6
star
37

tailwind-preset

Laravel TailwindCSS Preset extends the Laravel's UI command to add a new preset for TailwindCSS configuration, leveraged by AlpineJS.
Blade
6
star
38

laravel-ec2-metadata

Retrieve the EC2 Metadata using Laravel's eloquent syntax.
PHP
4
star
39

laravel-exporter-contracts

Base contracts implementation for Prometheus exports in Laravel.
PHP
4
star
40

blade-mdi

Material Design Icons for Laravel Blade views.
PHP
4
star
41

laravel-firebase-analytics

Laravel Firebase Analytics adds blade directives to initialize, log events and set user properties for Firebase Analytics.
PHP
4
star
42

laravel-chained-jobs-shared-data

Chained Jobs Shared Data is a package that helps you share some data (usually an array) between chained jobs.
PHP
3
star
43

echo-server-core

Echo Server Core is a Laravel utility package used for Socket.IO-based Echo Server application.
PHP
2
star
44

cloudflare-sdk

Cloudflare SDK generated automatically from the OpenAPI definitions.
PHP
1
star
45

blog

Blog backup for Hashnode.
1
star
46

laravel-eloquent-query-cache-docs

1
star