client->contacts; $contacts->each(function ($contact) use($quote){ $invitation = QuoteInvitation::whereCompanyId($quote->company_id) ->whereClientContactId($contact->id) ->whereQuoteId($quote->id) ->first(); if (!$invitation && $contact->send) { $ii = QuoteInvitationFactory::create($quote->company_id, $quote->user_id); $ii->quote_id = $quote->id; $ii->client_contact_id = $contact->id; $ii->save(); } elseif ($invitation && !$contact->send) { $invitation->delete(); } }); return $quote; } }