Git Product home page Git Product logo

friends's Introduction

Unmaintained

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

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:

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

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

friends's People

Contributors

arubacao avatar hootlex avatar irazasyed avatar nilportugues avatar stephane-monnot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.