37 lines
1.1 KiB
PHP
37 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V2;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\V2\StoreCustomerAnswers;
|
|
use App\Models\Customer;
|
|
use App\Models\Question;
|
|
|
|
class AnswerController extends Controller
|
|
{
|
|
/**
|
|
* Store the customer answers.
|
|
*
|
|
* @param \App\Http\Requests\Api\V2\StoreCustomerAnswers $request
|
|
* @param \App\Models\Customer $customer
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function storeCustomerAnswers(
|
|
StoreCustomerAnswers $request,
|
|
Customer $customer
|
|
): \Illuminate\Http\JsonResponse {
|
|
// * Make sure we are accessing a customer that belongs to our service.
|
|
$this->authorize('customerBelongsToService', $customer);
|
|
|
|
$question = Question::find($request->validated('question'));
|
|
|
|
$customer->answers()->detach(
|
|
collect($question->answers->modelKeys())->map(fn($id) => (string) $id)->toArray()
|
|
);
|
|
$customer->answers()->attach(
|
|
collect($request->validated('answers'))->map(fn($id) => (string) $id)->toArray()
|
|
);
|
|
|
|
return response()->json();
|
|
}
|
|
}
|