Search code examples
phplaravelphpmyadminlaravel-breeze

Using user data to calculate formulas in laravel


I am currently trying to find a way to use the data that is already on the users table to calculate the BMI of the user.

Many of the tutorials I see, they have a form, and a post, but what do I do if I already have the weight and height of the user?

I have tried building a controller like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


class IMCController extends Controller
{
    public function calculate(){


        $weight = Auth::user()->weight;
        $height = Auth::user()->height;

        $imc = $weight / ($height^2);


        return $imc;

    }
 
}

But when I call it on the view it says the class doesn't exist.


Solution

  • Add the following line before the Class definition

       use Auth;
    

    Get your current user height & weight using the following code:

          $weight = Auth::user()->weight;
          $height = Auth::user()->height;