arubacao/friends

Manage Friends in Laravel

2.0.0 2016-06-20 11:37 UTC

README

This package is unmaintained and was only a quick hacking around. Don't use in production code.

Friends (Laravel 5 Package)

Build Status Latest Version SensioLabsInsight Quality Score Code Coverage codecov Software License

Organise Friends and Relationships Between Users in Laravel and Lumen.

Friends provides everything you need to easily implement your own Facebook like Friend System.

Users can:

  • Send Friend Requests
  • Accept Friend Requests
  • Deny Friend Requests
  • Delete Friends

Contents

## Installation

For Laravel 5.*

Pull in Package with Composer

composer require arubacao/friends

Register Service Provider

Include the service provider inside config/app.php.

'providers' => [
    ...
    Arubacao\Friends\FriendsServiceProvider::class,
    ...
];

Run Migrations

Publish the migration and migrate the database

php artisan vendor:publish --provider="Arubacao\Friends\FriendsServiceProvider"
php artisan migrate

After the migration, 1 new table will be created:

friends — stores relationships/friendships between Users

The vendor:publish command will also create a friends.php file in your config directory.
The default configuration should work just fine for most applications.
Otherwise check out Configuration.

Prepare User Model

Include Friendable Trait in User Model


use Arubacao\Friends\Traits\Friendable;

class User extends Model
{
    use Friendable; // Add this trait to your model
    ...
}

And you are ready to go.

## Configuration

Configuration File friends.php (Optional)

Find friends.php in your config folder. Make sure you published the package beforehand.

user_model — This is the applications User model used by Friends. users_table — This is the applications users table name used by Friends. ## Usage ### Friend Requests #### Send Friend Request ```php $user->sendFriendRequestTo($recipient); ``` `$user` must be instance of `User` `$recipient` must be instance of `User`, `User` array or integer (User id) #### Accept Friend Request ```php $user->acceptFriendRequestFrom($sender); ``` `$user` must be instance of `User` `$sender` must be instance of `User`, `User` array or integer (User id) #### Deny Friend Request ```php $user->denyFriendRequestFrom($sender); ``` `$user` must be instance of `User` `$sender` must be instance of `User`, `User` array or integer (User id) ### My Friends #### Delete Friend ```php $user->deleteFriend($douchebag); ``` `$user` must be instance of `User` `$douchebag` must be instance of `User`, `User` array or integer (User id) #### Retrieve Friends - Get all friends of a user - `status` is always `1` *ACCEPTED*
$friends = $user->friends();

$user must be instance of User

$friends:

[{
	"id": 3,
	"name": "harri121",
	"created_at": "2016-06-18 19:08:45",
	"updated_at": "2016-06-18 19:08:45",
	"pivot": {
		"sender_id": 1,
		"recipient_id": 3,
		"created_at": "2016-06-19 19:53:27",
		"updated_at": "2016-06-19 22:56:40",
		"status": 1
	}
}]
#### Retrieve Incoming Friends - Get all users who send friend request to `$user` - `status` is always `0` *PENDING* - `recipient_id` is always `id` of `$user`
$friends = $user->incoming_friends();

$user must be instance of User

$friends:

[{
	"id": 3,
	"name": "ejoebstl",
	"created_at": "2016-06-18 19:08:45",
	"updated_at": "2016-06-18 19:08:45",
	"pivot": {
		"sender_id": 3,
		"recipient_id": 1,
		"created_at": "2016-06-19 19:53:27",
		"updated_at": "2016-06-19 22:56:40",
		"status": 0
	}
}]
#### Retrieve Any Friends **Remember:** > Just like in the real life a 'friend' or 'friendship' can be anything, also negative ;) Get all users who have any kind of friendship/relationship with $user
$friends = $user->any_friends();

$user must be instance of User

$friends:

[{
	"id": 3,
	"name": "harri121",
	"created_at": "2016-06-18 19:08:45",
	"updated_at": "2016-06-18 19:08:45",
	"pivot": {
		"sender_id": 1,
		"recipient_id": 3,
		"created_at": "2016-06-19 19:53:27",
		"updated_at": "2016-06-19 22:56:40",
		"status": 1
	}
},
{
	"id": 2,
	"name": "ejoebstl",
	"created_at": "2016-06-18 19:08:41",
	"updated_at": "2016-06-18 19:08:41",
	"pivot": {
		"recipient_id": 1,
		"sender_id": 2,
		"created_at": "2016-06-19 19:53:27",
		"updated_at": "2016-06-19 19:53:27",
		"status": 0
	}
}]
### Relationships #### Has Relationship With ```php $user->hasRelationshipWith($person, $status); ``` `$user` must be instance of `User` `$person` must be instance of `User`, `User` array or integer (User id) `$status` must be array of integers (`Status`) #### Get Relationship With ```php $user->getRelationshipWith($person, $status); ``` `$user` must be instance of `User` `$person` must be instance of `User`, `User` array or integer (User id) `$status` must be array of integers (`Status`) #### Has Pending Request From ```php $user->hasPendingRequestFrom($person); ``` `$user` must be instance of `User` `$person` must be instance of `User`, `User` array or integer (User id) ### Query Users Including Relationships
$users = \App\User::whereIn('id', [2,3,4])
      ->includeRelationshipsWith(1)
      ->get();

$users:

[{
	"id": 2,
	"name": "ejoebstl",
	"created_at": "2016-06-18 19:08:41",
	"updated_at": "2016-06-18 19:08:41",
	"friends_i_am_sender": [{
		"id": 1,
		"name": "arubacao",
		"created_at": "2016-06-18 19:08:35",
		"updated_at": "2016-06-18 19:08:35",
		"pivot": {
			"sender_id": 2,
			"recipient_id": 1,
			"created_at": "2016-06-19 19:53:27",
			"updated_at": "2016-06-19 19:53:27",
			"status": 0
		}
	}],
	"friends_i_am_recipient": []
},
{
	"id": 3,
	"name": "harri121",
	"created_at": "2016-06-18 19:08:45",
	"updated_at": "2016-06-18 19:08:45",
	"friends_i_am_sender": [],
	"friends_i_am_recipient": [{
		"id": 1,
		"name": "arubacao",
		"created_at": "2016-06-18 19:08:35",
		"updated_at": "2016-06-18 19:08:35",
		"pivot": {
			"recipient_id": 3,
			"sender_id": 1,
			"created_at": "2016-06-19 19:53:27",
			"updated_at": "2016-06-19 22:56:40",
			"status": 1
		}
	}]
},
{
	"id": 4,
	"name": "random_user",
	"created_at": "2016-06-19 19:55:25",
	"updated_at": "2016-06-19 19:55:25",
	"friends_i_am_sender": [],
	"friends_i_am_recipient": []
}]
## License

Friends is free software distributed under the terms of the MIT license.

Analytics