PHP code example of ghanem / friendship

1. Go to this page and download the library: Download ghanem/friendship library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ghanem / friendship example snippets


use Ghanem\Friendship\Contracts\Friendable as FriendableContract;
use Ghanem\Friendship\Traits\Friendable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements FriendableContract
{
    use Friendable;
}

$me->befriend($you);              // send a request (returns ?Friendship)
$you->acceptFriendRequest($me);   // only the recipient can accept
$you->denyFriendRequest($me);     // deny — $me may send a new request later
$me->unfriend($you);              // remove an accepted friendship

$me->block($you);     // works with or without a prior friendship
$me->unblock($you);   // only the blocker can unblock

$me->isFriendsWith($you);            // accepted friendship exists
$me->hasSentFriendRequestTo($you);   // outgoing pending request
$me->hasFriendRequestFrom($you);     // incoming pending request
$me->hasBlocked($you);               // $me blocked $you
$me->isBlockedBy($you);              // $you blocked $me

$me->getFriends();               // Collection of friend MODELS (accepted only)
$me->getFriends(perPage: 15);    // LengthAwarePaginator of friend models

$me->getFriendship($you);        // the Friendship row for the pair, or null
$me->getAllFriendships();        // every Friendship row involving $me
$me->getPendingFriendships();
$me->getAcceptedFriendships();
$me->getDeniedFriendships();
$me->getBlockedFriendships();

$me->getFriendRequests();        // incoming pending Friendship rows

$me->getMutualFriends($you);             // Collection of shared friends
$me->getMutualFriends($you, limit: 10);
$me->getMutualFriendsCount($you);
$me->getFriendsCount();
$me->getPendingRequestsCount();          // incoming pending requests

  Schema::table('friendships', function (Blueprint $table) {
      $table->unique(
          ['sender_id', 'sender_type', 'recipient_id', 'recipient_type'],
          'friendships_sender_recipient_unique'
      );
  });
  
bash
php artisan vendor:publish --tag=friendship-migrations
php artisan migrate