samedi 6 juin 2020

Optimise Laravel Eloquent Query Result

I have a homework table in laravel-5.7 where 5000 records in the table also have some Relational records that are coming through HasMany() or HasOne() Relation. I tried many types of Eloquent Queries to get fast results. but Postman result time becomes 10200ms to 10700ms but when i direct dispaly this into postman then i am getting this into 500ms to 1100ms. i want to get it in near about 800ms after binding form Laravel Resource or Normal Array.

problem is, when i try to show the Eloquent result direct then it coming around 600ms to 1000ms. but when i bind into an Array and display in postman then its taking 6200ms why? i do not know?

    $page = $req->page ?$req->page:1;  // set starting value for look query limit.
    $user = Auth::user()->student()->first();
    $studentProfile = Auth::user()->student()->first();

    // collecting all homework id that have assigned to student.
    $studentHWList =  StudentHomeWork::where("student_id",$studentProfile->id)
        ->select('home_work_id')
        ->get()->pluck('home_work_id');

   // collecting page by page of homework id.
    $hwLimitList =  Homework::where('session_code', dnc($req->require('sid')))
            ->whereIn('id',$studentHWList )
            ->where('approved', '1')
            ->select('id')
            ->orderBy('updated_at','desc')
            ->get();
    $hwIndexes = $hwLimitList->pluck('id')->forPage($page,$this->recordLimit);

    $paginated = Homework::whereIn('id', $hwIndexes)
                        ->with( "user:id,username,name",
                                'subject:id,subject_name,subject_code',
                                'approveByUser','publishBy')
                        ->with(["likes"=>function($erw){
                                 $erw->select('id','home_work_id','complete_status','likes')
                                ->where("student_id", $studentProfile->id);
                        }])
                        ->with(['comment'=>function($qur){
                            $qur->where('parent_id',0)
                                ->where('user_id',$user->id);
                        }])
                        ->orderBy('id','desc')
                        ->get( );

    if( count($paginated))
    {
        $paginationData =  customPagination('getAllHW',$hwLimitList , $page , $this->recordLimit , $user, $studentProfile  );
        return response()->json(["error"=>0,"errmsg"=>"","paginationData"=>$paginationData  ,
            "response"=>['homework_list'=>$this->customResourceHWBinding($paginated , $req )],'auth'=>userType()]);



private function customResourceHWBinding($queryData , $request, $user, $studentProfile )
{
    $document_list =[]; $is_seen=0; $resultData =[];
  foreach ( $queryData as  $query )
  {
      if( count($query->document)  )
      {
          foreach($query->document as $document){
              if( $document->changed_filename )
              {
                  $file=""; $fileName ="";
                  $path =env('AWS_URL')."/uploads/".dnc($request->header('dbauth'))."/".$query->session_code."/homeWorks/";
                  if(is_s3FileExist( $path.$document->changed_filename ) )
                  {
                      $fileName =$document->changed_filename;

                  }
                  $document_list[] = [
                      'oname'=> $document->changed_filename,
                      'ext'=>$fileName?explode('.', $document->changed_filename):"",
                      'url'=>$file,
                      'file_url'=>$document->changed_filename?$path.$document->changed_filename:""
                  ];
              }
          }
      }

      $resultData[] =   [
      'id'=>enc($query->id),
      'ids'=>$query->id,
      'pin_user_id'=>"",
      'pin_enabled'=>0,

      'created_by'=>$query->user->name,
      'created_by_image'=>getUserImage($query->user,$query->user->privilege,$request),
      'assignment_date'=>getDateFormat($query->assignment_date,0),
      'assigment_date_edit'=>"",
      'submission_date'=>getDateFormat($query->submission_date,1),
      'submission_date_edit'=>"",
      'class_code'=>$query->class_code,
      'subject'=>$query->subject?$query->subject->subject_name:"",   
      'topic'=>$query->topic,
      'is_student_seen'=> $this->studentHWSeen($query, $user, $studentProfile),
      'updated_at'=> date('d-m-Y H:i:s' , strtotime($query->updated_at)),
      'approved'=>$query->approved,
      'approve_by'=> '',
      'can_approve'=>0,
      'comment_count'=>0,
      'total_like'=>0,
      'documents_count'=>count($document_list)?count($document_list):0,
      'is_draft'=> $query->draft?$query->draft:0,
  ];
  }
  return $resultData;
}

private function studentHWSeen( $query , $user, $studentProfile)
{
    if(count($query->studentSeen))
    {
        foreach($query->studentSeen as $seen){
            if( user->privilege == 1  )
            {
                if($seen->student_id == $studentProfile->id )
                   return 1;
            }
        }
    }
    return 0;

}

I try to use Resource But also taking 3+secconds. i try too many others optimize a solution but not work in my case. someone told Use query builder instead of Eloquent to optimize queries. find here Optimising Laravel query . is it a good answer for me? i am not sure. please help me.

please check my image.enter image description here

Eloquent Query Result enter image description here



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3eWTLI2
via IFTTT

Aucun commentaire:

Enregistrer un commentaire