James Ambardnishvili
James Ambardnishvili

Reputation: 43

Laravel, simplifying two Eloquent queries into one

Can i simplify this two queries into one, i have my versions which works but i think it can be done more simpler

public function removeFriend($userId)
{
   $user = User::findOrFail($userId);
   $user_id = $user->id;

   $me = \Auth::user();
   $me_id = $me->id;


   //DB::table('friends')->where(['friend_id' => $me_id, 'user_id' => $user_id, 'accepted' => '1'])->delete();
   //DB::table('friends')->where(['friend_id' => $user_id, 'user_id' => $me_id, 'accepted' => '1'])->delete();

   //$first = DB::table('friends')->where(['friend_id' => $me_id, 'user_id' => $user_id, 'accepted' => '1']);
   //DB::table('friends')->where(['friend_id' => $user_id, 'user_id' => $me_id, 'accepted' => '1'])->union($first)->delete();



   return redirect()->back();
} 

commented lines are the two queries, below is the one that i researched but ithink its kinda same thing, basically i tried to join second query with orWhere(), but it gets all other friends too, and if i join them with only where() it doesn't get anything so that's a bust

Upvotes: 0

Views: 76

Answers (2)

Rutvij Kothari
Rutvij Kothari

Reputation: 1285

Based on your question I am assuming you want to implement "Unfriend" feature.

Let's just update your code.

public function removeFriend($userId)
{
    // This is not necessary. You are making one more query which is doing nothing basically. You are getting $userID which is same as $user->id
    //$user = User::findOrFail($userId);
    //$user_id = $user->id;

    //DB::table('friends')->where(['friend_id' => $me_id, 'user_id' => $user_id, 'accepted' => '1'])->delete();
    //DB::table('friends')->where(['friend_id' => $user_id, 'user_id' => $me_id, 'accepted' => '1'])->delete();

    //$first = DB::table('friends')->where(['friend_id' => $me_id, 'user_id' => $user_id, 'accepted' => '1']);
    //DB::table('friends')->where(['friend_id' => $user_id, 'user_id' => $me_id, 'accepted' => '1'])->union($first)->delete();

    // Better Approach
    try {
        $result DB::table('friends')
            ->where(function($query) use($userID) {
                $query->where('friend_id', $userID)->where('user_id', Auth::id());    
            })->orWhere(function($query) use($userID) {
                $query->where('user_id', $userID)->where('friend_id', Auth::id());
            })
            ->delete();

        //Do whatever you want woth result
    }
    catch(/Exception $e) {
        // Exception Handling;
    }          

    return redirect()->back();
} 

Note: Not Tested.

Upvotes: 1

Diogo Santo
Diogo Santo

Reputation: 789

For you to be building the queries that way means your ids are indeed different. If such is the case, you can use the orWhere as an advanced query to add both values and get 2 rows that you can cycle and delete. But because they are different you will never have 2 rows merged into 1 and be able to use delete like that. It does not come out as an object when you start joining things:

Friend::where('accepted', '=' 1)->where( function($query) use($me_id, $user_id) {
                                      $query->where('friend_id', '=', $user_id)
                                            ->orWhere('friend_id', '=', $me_id);
                                 })->get();

Upvotes: 1

Related Questions