List 4 bank tables and account

Asked

Viewed 42 times

1

Hello I have 4 tables: Employees, Companies, Salaries and Discounts. I would like to present a kind of Holerite. My initial idea was to bring all the information together and treat the view as follows:

(OBS: I’m using Laravel Slide)

@foreach ($funcionarios as $funcionario)
  @php
    $total_vencimentos = 0;
    $total_descontos = 0;
    $total_liquido = 0;
  @endphp
  @foreach ($vencimentos as $vencimento)
    @if ($funcionario->id == $vencimento->funcionario_id)
      imprime os vencimentos
    @endif
    @php $total_vencimentos += $vencimento->valor_adicionado @endphp
  @endforeach
  @foreach ($descontos as $desconto)
    @if ($funcionario->id == $desconto->funcionario_id)
      imprime os vencimentos
    @endif
    @php $total_vencimentos += $desconto->valor_descontado @endphp
  @endforeach
  @php
    $total_liquido = ($total_vencimentos - $total_descontos);
  @endphp
@endforeach

This view is generating a PDF. This code is working well, but I worry if this process can get heavy with the increase of employees in the company, there is a better solution ?

Thank you.

1 answer

1


Let’s go to a few points, with the increase in the number of employees of course that can be slower, but I believe you should worry less about the volume of data and more about how to search for and treat them. For example, there are ways to mount queries that return Gigas of data in seconds, but what greatly influences performance as well, is how to handle the data after the database query. I’m going to talk a little bit about good practices for MVC that will improve performance, nothing very technical in development...

You quoted: bring all the information together and treat in view.

Look at the problem of doing this... You are using a MVC framework (Model - View - Controller), one of the biggest sins you can commit is to "treat" the data in the visualization file (View), because this is what the Controllers are for, you will leave the data ready and only send to View, even if in the last case, try to minimize the ties of foreachs by sending more complete objects to View instead of going through several arrays of objects within it. You can solve this with some Joins in the Controller query. After that you can test various forms of queries and treatments in the Controller, even using Models to optimize searches (Laravel makes it much easier to do so). Using the basic good practices of a MVC frame will already give you a significant improvement in performance and at the end will notice that the least concern will be the data volume of the database.

This is a simple answer that can be complemented, additional suggestions are welcome.

  • Thanks for the recommendations, I’ll bring the logic to the controller. But in the matter of functioning I was still a little confused. After all if I perform the query using Join, only one line each employee would return and what I need is the employee -> company -> ALL salaries and ALL discounts. For this reason called separate. It is possible to make this query in a single query ?

  • If Voce has a key in common in the tables is possible yes, in this case I believe it has a field with id of the company or employee in all tables then it would be much easier, if you need help in the assembly of the query I think would fit until open another topic to help us.

  • I have the key, and yes the doubt was really on how to mount. I will open a new Question. Thank you...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.