0

My laravel json resource is here

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\SurveyResponse;
class SurveyQuestionsCollection extends JsonResource
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        if(isset($this->id)) {
            return [
                'id' => $this->id,
                'survey_id' => $this->survey_id,
                'question' => $this->question,
                'sequence' => $this->sequence,
                'type' => $this->type,
                'options' => $this->options,
                'selected_option'=>SurveyResponse::where('question_id',$this->id)->first()->pluck('answer'),
            ];
        } else {
            return [];
        }
    }
}

and response is this

{
    "statusCode": 200,
    "message": "Survey fetch successfully...",
    "data": {
        "user_survey_id": 1,
        "questions": [
            {
                "id": 1,
                "survey_id": 1,
                "question": "what is current version of Bootsarap  ?",
                "sequence": "1",
                "type": "Singleselect",
                "options": "3,4,5,6",
                "selected_option": [
                    "Options 1"
                ]
            }
        ]
    }
}

And i need this response

{
    "statusCode": 200,
    "message": "Survey fetch successfully...",
    "data": {
        "user_survey_id": 1,
        "questions": [
            {
                "id": 1,
                "survey_id": 1,
                "question": "what is current version of Bootsarap  ?",
                "sequence": "1",
                "type": "Singleselect",
                "options": "3,4,5,6",
                "selected_option": "Options 1"
                
            }
        ]
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • `SurveyResponse::where('question_id',$this->id)->first()->pluck('answer')->toArray();` or use `get()` method `SurveyResponse::where('question_id',$this->id)->get()->pluck('answer')->toArray();` – Dilip Hirapara Sep 29 '20 at 12:39
  • @RiggsFolly data is dyanmic it can be multiple arrray – Vikas Singh Sep 29 '20 at 12:39
  • 1
    If `"selected_option":` Can be an array, then you have to leave it as it is! because `"selected_option": "Options 1"` Is not an array – RiggsFolly Sep 29 '20 at 12:41
  • @Dilip please tell me who told you that it was okay to break the very obvious page design by posting solutions as comments. https://meta.stackexchange.com/a/296481/352329 – mickmackusa Sep 29 '20 at 12:41

1 Answers1

0
SurveyResponse::where('question_id', $this->id)->first()->answer

first() fucntion returns object, and then you get field answer throught arrow, if every question had selected option, otherwise you have to check if object exists

V-K
  • 1,297
  • 10
  • 28
  • 1
    "Do it like this" answers are low-value on Stack Overflow because they do very little to educate/empower the OP and thousands of future researchers. Before answering a basic question consider the fact that all basic questions have been asked and answered here already -- in which case, the question should be closed instead of answered. – mickmackusa Sep 29 '20 at 12:44
  • it is giving error Trying to get property 'answer' of non-object – Vikas Singh Sep 29 '20 at 13:10