Call model from associate table

Posted 1 month ago by MarlonV

Goodmorning all, question. I want to know if the total payment of a user. It's already working in the payment model. How do i use/call it in my check-in controller?

    public function checkin(Request $request)
    {
          $validatedData = $request->validate([
            'barcode' => 'required|max:10',
         ]);

         $child = Child::where('barcode', $request->barcode)->with('todaysEvents')->first();

 
         if ($child->user->payments->usrAmountdue($userId) == 0) {

            
            return redirect()->route('admin.checkins.index')->with('danger', 'Not payd');     
        }

    ...

    }

payment.Model


    public static function usrTotalPayd($userId)
    {
        return Payment::whereUserId($userId)->Totalbillyear()->sum('amount_payd');
    }

    public function usrTotalPrice()
    {
        $sum=0.00;
        foreach($this->user->children as $child) {
            foreach($child->events as $event) {
    
                $sum+= $event->price;   
            }  
        }   
        return $sum;
    }

    public function usrAmountdue($userId)
    {
        
        $amountdue =  $this->usrTotalPrice() - $this->usrTotalPayd($userId);

        return $amountdue;
    }

Please sign in or create an account to participate in this conversation.