bennywhitebread
5 months ago

To show statistics (percentage), better to work with queries or to work on Collection ?

Posted 5 months ago by bennywhitebread

Hi all !

I have an app with Many-to-many relationships (Trainings - Users) and I'd like to grab the percentage of users that have completed a training. The pivot field is status (1 for completed and 0 when it's not completed)

My question is more to know the best practice to get it : by using a query or by iterating the object.

Since I already get the query to grab all data and users associated to a training. My first thaught is to make a function to loop into the object receivde by using

$training = 
Training::with('users')->find($id);

and to iterate the training object and count each status, then pass the percentage for this training to the view

Is there a better option using Eloquent ?

Thank you

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