get(); $lineContactIsToDelete = collect([]); $lineOfContactToDelete = collect([]); foreach ($relationships as $relationship) { $contact = DB::table('contacts')->where('id', $relationship->contact_is)->first(); if (! $contact) { $lineContactIsToDelete->push($relationship); } $contact = DB::table('contacts')->where('id', $relationship->of_contact)->first(); if (! $contact) { $lineOfContactToDelete->push($relationship); } } foreach ($lineContactIsToDelete as $relationship) { DB::table('relationships')->where('id', $relationship->id)->delete(); } foreach ($lineOfContactToDelete as $relationship) { DB::table('relationships')->where('id', $relationship->id)->delete(); } } }