'How to get total earning of therapist in laravel?

I want to calculate total earning of therapist for i have three table session , session_request, commission. In session table we have fields id, therapist_id, session_request_id, status(completed,pending). In session_request table i have id, type[audio,video] . In commission table per_audio_cost:2 and per_video_cost:1. Those who have status completed find their total earning on basis of audio and video call. Please help how to do this in laravel.

public function index(Request $request)
    {
      
        try{
            $title="User";
            if(isset($request->uid)){
                $uid = $request->uid;
                $therapist = User::where('id',decrypt($uid))->first();
                $therapist_commission = TherapistCommission::first();
               
                $sessions = Session::where('therapist_id',decrypt($request->uid))->with('session_request')->latest()->get()->toArray();
                $s_req = SessionRequest::where('thearipist_id',decrypt($request->uid))->get()->toArray();

                foreach($s_req as $key => $val){

                    if($val['type'] == "1" ){
                        $count  = Session::where('therapist_id',$val['thearipist_id'])->where('status','2')->count();
                        dd($count);
                        $f_count = $count*2;
                    }else{
                        $count  = Session::where('therapist_id',$val['thearipist_id'])->where('status','2')->count();
                         $f_count2 = $count*1;
                        
                    }
                    
                    $final = @$f_count + @$f_count2; 

                    $s_req['nidi'][$key] = $final; 

                    
                }

              
               // dd($s_req);


            }else{
                // $sessions = Session::latest()->get();
                if(isset($request->title) && $request->title !=''){
                     $name =  $request->title;
                     $sessions = Session::latest()
                                 ->where(function ($title) use ($request) {
                                 $title->where('session_title', "like", "%" . $request->title . "%");
                                     
                                 })->get();   
  
                }
            }
                if(isset($request->status) && $request->status !='' ){
                $sessions = Session::where('status',$request->status)->get();  
                }
                if(isset($request->type) && $request->type !='' ){
                    $sessions = Session::whereRelation('session_request', 'type', '=', $request->type)->get();
                }
            
             
         }
        catch(\Exception $e){
            abort('404', $e->getMessage());
        }
        if(isset($request->uid)){
            return view('admin.sessions.index', compact('sessions','uid','therapist','therapist_commission'));
        }else{
            return view('admin.sessions.index', compact('sessions'));
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source