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"
                
            }
        ]
    }
}
4
  • SurveyResponse::where('question_id',$this->id)->first()->pluck('answer')->toArray(); or use get() method SurveyResponse::where('question_id',$this->id)->get()->pluck('answer')->toArray(); Commented Sep 29, 2020 at 12:39
  • @RiggsFolly data is dyanmic it can be multiple arrray Commented Sep 29, 2020 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 Commented Sep 29, 2020 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. meta.stackexchange.com/a/296481/352329 Commented Sep 29, 2020 at 12:41

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

"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.
it is giving error Trying to get property 'answer' of non-object

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.