G
G
georgich2020-04-26 20:34:17
Twig
georgich, 2020-04-26 20:34:17

How to implement schedule display via TWIG?

Greetings!
I use Slim + Eloquent ORM + Twig.
There is a schedule model:

class Schedule extends Model
{
    public function employee()
    {
        return $this->belongsTo(Employee::class);
    }
}

I get the schedule like this and pass the schedules variable to the template:
$schedules = Schedule::all();
In the schedules variable, the data is as follows:
{"employee_id":260,"start_date":"2020-04-26","start_time":"16:14:39","end_time":"20:14:44"},
{"employee_id":20,"start_date":"2020-04-28","start_time":"16:14:39","end_time":"20:14:44"},
{"employee_id":260,"start_date":"2020-04-29","start_time":"16:14:39","end_time":"20:14:44"},
{"employee_id":20,"start_date":"2020-04-30","start_time":"16:14:39","end_time":"20:14:44"},
{"employee_id":20,"start_date":"2020-05-02","start_time":"16:14:39","end_time":"20:14:44"},
{"employee_id":260,"start_date":"2020-05-02","start_time":"16:14:39","end_time":"20:14:44"}

The schedule view on the page should look like this:
<table>
  <tr>
    <th>Сотрудник</th>
    <th>26.04</th>
    <th>27.04</th>
    <th>28.04</th>
    <th>29.04</th>
    <th>30.04</th>
    <th>01.05</th>
    <th>02.05</th>
  </tr>
  <tr>
    <td>260</td>
    <td>16:14 - 20:14</td>
    <td></td>
    <td></td>
    <td>16:14 - 20:14</td>
    <td></td>
    <td></td>
    <td>16:14 - 20:14</td>
  </tr>
  <tr>
    <td>20</td>
    <td></td>
    <td></td>
    <td>16:14 - 20:14</td>
    <td></td>
    <td>16:14 - 20:14</td>
    <td></td>
    <td>16:14 - 20:14</td>
  </tr>
</table>

and visually:
5ea5c545c7ab7566122865.png
So the question is: how to implement the output of such a schedule structure in twig? The problem is duplicate employee_id with different dates. Respectively, in the Employee column there should be no repetitions of employee_id, and the dates should be filled in by columns.
Hope you get the idea. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2020-04-26
@glaphire

You need to parse and organize this data yourself so that you can transfer data from the controller to the template in the form of an array suitable for generating as a table. Move this logic into a separate class, and in the controller, call only the method that converts the raw data to the desired format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question