Good day!

Please tell me how to make a selection from the link table for the current date? For example, there is a table of managers in which the name, department, and general plan are indicated. And there is a sales table, in which there are fields the name, amount, and choice of the manager. Each manager can make multiple sales.

I need to receive in the controller those sales that are not shown on the current date, for the last month.

I now get the data like this:

$managers = Manager::with('sales')->get(); 

And in the view I display it like this:

 @forelse ( $managers as $manager ) <tr> <td>{{ $manager->name }}</td> <td>{{ $manager->department }}</td> <td>{{ $manager->overall_plan }}</td> <td>{{ $manager->sales->sum('summa') }}</td> <td> <progress class="progress is-success" id="progress" min="0" max="100" value="{{$manager->sales->sum('summa') / $manager->overall_plan * 100}}"> </progress> <span class="percent">{{ round( $manager->sales->sum('summa') / $manager->overall_plan * 100 ) }}&nbsp;%</span> </td> </tr> @empty <h3>Нет данных</h3> @endforelse 

This is how it is displayed. Main table

It would be desirable that only sales of the current month would be displayed and the past ones would not be displayed.

The fact is that one manager can have several sales, how can I make such a request so that I can take data only on the current date? Or what other way is more correct?

The managers table has a hasMany relationship with sales, and sales has an inverse belongsTo relationship with the Manager.

Here is the managers page

Managers page

Here is the sales page enter image description here

Here is a form for adding a manager enter image description here

Sales Adding Form enter image description here

In the form of adding a sale manager is selected from the table manager.

  • To make a selection of sales for the current day, you must first store the date of the sale. And then retrieve the entries according to this condition. - Sergey
  • Each sale has a field created_at, I try to get data based on what date. But not yet. Those. I get all the data like this: $managers = Manager::with('sales')->get(); I have a collection of managers here, and every manager in the collection has a relations field, and in it there is a collection of the Sale model. Some managers have several sales. Correspondingly, in the collection of managers, there is a collection with several sales. Do I need to run through them foreach and do a logical check on the field created_at for each sale in a cycle? I can't figure out how to do it here - Dolphin

1 answer 1

And if you make a condition in a cycle in a view and sift there? That is something like this:

 @forelse ( $managers as $manager ) @if ($manager->sales->created_at > \Carbon\Carbon::now()->startOfDay()) <tr> <td>{{ $manager->name }}</td> <td>{{ $manager->department }}</td> <td>{{ $manager->overall_plan }}</td> <td>{{ $manager->sales->sum('summa') }}</td> <td> <progress class="progress is-success" id="progress" min="0" max="100" value="{{$manager->sales->sum('summa') / $manager->overall_plan * 100}}"> </progress> <span class="percent">{{ round( $manager->sales->sum('summa') / $manager->overall_plan * 100 ) }}&nbsp;%</span> </td> </tr> @endif @empty <h3>Нет данных</h3> @endforelse 

This is if you need sales for the current day.

  • Well, as an option, you can also use. But I solved this issue differently. - Dolphin
  • My solution: 1) Added the month field to the sales table. 2) In the sales creation view, added <input type = "hidden" name = "month" value = "{{Carbon \ Carbon :: now () -> format ('m')}}">. Next in the month field will get the current month. 3) In the controller, it changed the data acquisition: $ managers = Manager :: with (['sales' => function ($ query) {$ query-> where (' month ', Carbon :: now () -> format (' m '));}]) -> get (); And that's all, I get sales only for the current month and in the view I don’t have anything to do with shamanism with logic. - Dolphin