Peter Arents
Peter Arents

Reputation: 27

sql delete query with where, and, or laravel

Hello i am building a delete query to delete an row out of my database in laravel and i came to this problem i need to find a row where ... and ... or ... and ...

So in normal php that would be

DELETE * FROM connection_requests 
WHERE `user_id`=$user->id AND `selected_user_id`=$id OR 
`selected_user_id`=$user->id AND `user_id`=$id;

(The first and belongs to the where and the second and belongs to the or)

now how do i do this in laravel

Upvotes: 0

Views: 5130

Answers (3)

ram pratap singh
ram pratap singh

Reputation: 53

this would be work try its

  DB::table('connection_requests')
          ->where(['user_id'=>$user->id, 'selected_user_id'=>$id])
          ->orWhere(['user_id'=>$id, 'selected_user_id'=>$user->id])
          ->delete();

Upvotes: 2

Peter Arents
Peter Arents

Reputation: 27

i fixed it myself everyone thanks for thinking with me

 DB::table('connection_requests')
        ->where('user_id', $user->id)
        ->where('selected_user_id', $id)
        ->orWhere('selected_user_id', $user->id)
        ->orWhere('user_id', $id)
        ->delete();

Upvotes: 0

linktoahref
linktoahref

Reputation: 7972

You may try this

DB::table('connection_requests')
    ->where(function ($query) use ($user->id, $id) {
        $query->where('user_id', '=', $user->id)
              ->where('selected_user_id', '=', $id);
    })
    ->orWhere(function ($query) use ($user->id, $id) {
            $query->where('selected_user_id', '=', $user->id)
                  ->where('user_id', '=', $id);
    })
    ->delete();

Upvotes: 0

Related Questions