authorize('customerBelongsToService', $customer); $question = Question::find($request->validated('question')); // Column model_id is varchar because of singleoptin uuid, so force cast in string. ModelAnswer::query() ->where('model_type', Customer::class) ->where('model_id', (string) $customer->getKey()) ->whereIn('answer_id', $question->answers->modelKeys()) ->delete(); foreach ($request->validated('answers') as $answerId) { ModelAnswer::create([ 'model_type' => Customer::class, 'model_id' => (string) $customer->getKey(), 'answer_id' => $answerId, ]); } return response()->json(); } }