Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jagdishabhiandroid's avatar

How to add more elements to a resource data collection in REST API

i want to add more elements from different table to a resource data collection i have created first how can i do this with pagination here is my current code:

public function seefav(Request $request)
    {
      
      
      
      
      $validator = \Validator::make($request->all(), [
        'student_id'=> 'required',
             ]);
         if ($validator->fails()) {
            
            return Response::json(['message' => 'Empty values not allowed']);
        }else{
            
            
      if (favourites::where('student_id', $request->student_id)->exists()) {
          
          
          
        $student = favourites::where('student_id', $request->student_id)->get();
       
       
        foreach($student as $st){ 
             $quiz=$st->quiz_id;
            
            
        
        
       
        $resultsquiz = DB::select( DB::raw("SELECT * FROM quizzes WHERE quiz_id ='$quiz'") );
 
 foreach($resultsquiz as $resquiz){
     
      $quiz_image=asset($resquiz->quiz_image);
       $quiz_time=$resquiz->question_time;
       $life=$resquiz->life;
       $quiz_best=$resquiz->best_score;
       $quiz_name=$resquiz->quiz_name;
      $quizdata=array("quiz_name"=>$quiz_name,"quiz_image"=>$quiz_image); 
       
       
  }
        }
  
   return Response::json(['quiz' => $quizdata,'data'=>new FavouritesResource($student)]);  
   

///these comment code i tried but its not as i want the response in default data collection


        //return FavouritesResource::collection($student);
       //return FavouritesResource::collection($student)->additional([
            //'data' => [
           //     'id'=>$id,
            //    'quiz_name' => $quiz_name
           // ]
        //]); 
       
        
        
      }else{
      return Response::json(['message' => 'NO data found ']); 
    }
    
        }
} ```  
0 likes
2 replies
aurawindsurfing's avatar

Before sending your reponse create one data array/collection that has everything in it. Do json_encode on it. Attach it to your reponse.

Please or to participate in this conversation.