diff --git a/app/Http/Controllers/Api/V2/QuestionController.php b/app/Http/Controllers/Api/V2/QuestionController.php index 3d53a6e..6c3246a 100644 --- a/app/Http/Controllers/Api/V2/QuestionController.php +++ b/app/Http/Controllers/Api/V2/QuestionController.php @@ -38,13 +38,7 @@ class QuestionController extends Controller $customerAnswers = Answer::whereHas('customers', function ($q) use ($customer) { $q->whereId($customer->id); })->get(); - $customerAnswers = Answer::whereHas('customers', function ($q) use ($customer) { - $q->whereId($customer->id); - })->whereHas('question', function ($q) use ($service) { - $q->whereHas('brand', function ($q) use ($service) { - $q->whereId($service->brand->id); - }); - })->get(); + $customerAnswers = $customer->getAnswers($service); return new QuestionsResource($service->brand->questions, $customerAnswers); } diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 935da9c..f490958 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -3,6 +3,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -103,4 +104,22 @@ class Customer extends Model $optInHistory->new_value = $optIn; $optInHistory->saveOrFail(); } + + /** + * Get All answers given by this customer + * for a specific service + * + * @param Service $service + * @return Collection + */ + public function getAnswers(Service $service): Collection + { + return Answer::whereHas('customers', function ($q) { + $q->whereId($this->id); + })->whereHas('question', function ($q) use ($service) { + $q->whereHas('brand', function ($q) use ($service) { + $q->whereId($service->brand->id); + }); + })->get(); + } }