'required|integer|exists:accounts,id', 'call_id' => 'required|integer', ]; } /** * Destroy a call. * * @param array $data * @return bool */ public function execute(array $data): bool { $this->validate($data); $call = Call::where('account_id', $data['account_id']) ->findOrFail($data['call_id']); $contact = $call->contact; $contact->throwInactive(); // delete all associations with emotions $call->emotions()->sync([]); $call->delete(); $this->updateLastCallInfo($contact); return true; } /** * Update last call information of the contact. * * @param Contact $contact * @return void */ private function updateLastCallInfo(Contact $contact) { // look for all the calls of the contact and take the most recent call // as the one we just deleted could have been the most recent call $contact->last_talked_to = optional($contact->calls->first())->called_at; $contact->save(); } }