how to get users friend birthdays list

Published 2 months ago by Dev0ps

i have a birthday page where i show all friends list along with there birthday

i create a code which gives me friends id. now how to check

$friendships = Friend::whereStatus(2)->whereUserId(Auth::id())->get(['user_id', 'friend_id']);
            $friend_id  = $friendships->pluck('friend_id')->all();
            $user_id     = $friendships->pluck('user_id')->all();
            $myFriends = Friend::where('friend_id', '!=', Auth::id())->whereIn('friend_id', array_merge($friend_id))->whereIn('user_id', array_merge($user_id))->get();
            $myFriends = $myFriends->pluck('friend_id')->toArray();
            $data['friends'] = $myFriends;

currently i use this

@foreach(\App\User::whereMonth('birthday', 6)->get() as $Friend)
                        <div class="birthday-item inline-items">
                            <div class="author-thumb">
                                <img src="{{ asset(\App\Avatar::orderBy('id','desc')->where('username', '=', $Friend->username)->first()->source)}}" height="36" width="36" alt="author">
                            </div>
                            <div class="birthday-author-name">
                                <a href="#" class="h6 author-name">{{$Friend->first_name. ' ' .$Friend->last_name}} </a>
                                <div class="birthday-date">{{Carbon\Carbon::parse($Friend->birthday)->toFormattedDateString()}}</div>
                            </div>
                            <a href="20-CalendarAndEvents-MonthlyCalendar" class="btn btn-sm bg-blue">Create Event</a>
                        </div>

but it shows all users birthday :( not only friends

Best Answer (As Selected By Dev0ps)
lostdreamer_nl

you were not logged in

$data['friends'] = (auth()->check() ?  auth()->user()->friends->load('avatars') : collect());
Sergiu17

You should have a relationship on user model called friends next

$myFriends = auth()->user()->friends;
foreach($myFriends as $friend)
{
    // print birthday's friend
}
lostdreamer_nl

You also had a nasty N+1 query problem with the avatars for those users so I also have a relation for the avatars here so you can eager load them.

// Class User.php
public function friends() 
{
    return $this->belongsToMany(User::class, 'friends', 'user_id', 'friend_id');
}
public function avatars() 
{
    return $this->hasMany(Avatar::class, 'username', 'username');
}

// controller:
$data['friends'] = auth()->user()->friends->load('avatars');



// view
@foreach($friends as $friend)
    <div class="birthday-item inline-items">
        <div class="author-thumb">
            <img src="{{ asset($friend->avatars->first()->source)}}" height="36" width="36" alt="author">
        </div>
        <div class="birthday-author-name">
            <a href="#" class="h6 author-name">{{$friend->first_name. ' ' .$friend->last_name}} </a>
            <div class="birthday-date">{{Carbon\Carbon::parse($friend->birthday)->toFormattedDateString()}}</div>
        </div>
        <a href="20-CalendarAndEvents-MonthlyCalendar" class="btn btn-sm bg-blue">Create Event</a>
    </div>
@endforeach

btw: You should also fix that relationship between User and Avatar, add the user_id to the avatars table and use that instead of the username, you should not use varchar fields within relationships ;)

Dev0ps

oops!

ErrorException
Trying to get property friends of non-object
lostdreamer_nl

you were not logged in

$data['friends'] = (auth()->check() ?  auth()->user()->friends->load('avatars') : collect());

Please sign in or create an account to participate in this conversation.