skip to Main Content

I am currently building an app with laravel 11 and need to run a function that check’s a user’s available balance. To get the result of the available balance, I need to grab the user’s "credit limit" (stored inside user’s table) and subtract it with the month’s transactions (inside a transaction table) amounts.

So question is:
Where should I include a function to do the math calculation so that the template views have the total available balance displayed. I wish to make this user specific so the function doesn’t need to run for admins, managers, etc. Only need to run the function for a specific role type (general user’s). The available balance would be displayed in the header so for every page for general user’s.

Should I use Helper files? Middleware? Providers? Still a bit confused on what the most efficient route would be. Any insight would be greatly appreciated.

Thank you!

Have not tried anything yet but I think it would be towards Providers.

2

Answers


  1. I suggest that you make new user field "available_balance".

    Then you make observers for transactions and every other model that is part of calculating balance (e.g. user credit limit).

    In this solution you are calculating balance only when there is an actual change.

    Login or Signup to reply.
  2. I’m not sure how you store your role or permission. try spatie/laravel-permission this package can check what role or permission of each user by using User model

    then when you can get role/permission by using

    $user = Auth::user();
    if($user->hasRole('General User')){
       $balance = $user->credit_limit;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search