Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mohammed's avatar

Getting my friends and friend requested and not my friends on a query

Hi there, I am using this package rennokki/befriended https://github.com/renoki-co/befriended#filtering-followedunfollowed-models

And I implemented a search functionality on my React native app. But I want to get the requested friends and my friends also not my friends.

this is my controller

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;

class SearchFriendsController extends Controller
{
    public function index(Request $request, $query)
    {
        return User::where('username', 'LIKE', "%$query%")
        ->where(function($q) use ($request) {
            $request->user()->isFollowing($q);
        })
        ->get();

    }
}

User Model:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Rennokki\Befriended\Traits\Follow;
use Rennokki\Befriended\Contracts\Following;
use Rennokki\Befriended\Scopes\FollowFilterable;

class User extends Authenticatable implements Following
{
    use HasApiTokens, Notifiable, Follow, FollowFilterable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'avatar', 'location',
        'username', 'gender', 'bio', 'phone', 'birthday'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

  
    public function posts()
    {
        return $this->hasMany('App\UserPost');
    }

    public function userPostCount()
    {
        return $this->posts()->count();
    }

    public function getAvatarAttribute($value)
    {
        if ($value !== NULL) {
            return asset('avatars/' . $value);
        }
        return NULL;
    }
}


I want I way to check if he is my friend or not and if the i have sent a request.

Thanks.

0 likes
1 reply

Please or to participate in this conversation.