Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rawatashwin's avatar

Code Clean up question

Hi everyone,

I am making an app to setup meetings between two people across different tables and timeslots. I have come up with the following solution:

Meetings model

        Schema::create('meetings', function (Blueprint $table) {
            $table->id();
            $table->integer('table')->nullable();
            $table->integer('slot')->nullable();
            $table->foreignIdFor(Customer::class)->nullable();
            $table->timestamps();
        });

The controller

        $meetings = Meetings::join('customers', 'customer_id', '=', 'customers.id')->select('meetings.table', 'meetings.slot', 'customers.phoneno', 'customers.name')->get();

        $meets = [
            'Table 1' => [
                'Slot 1' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 1)->where('slot', 1)->slice(0, 1)->first()->name ?? '',
                        'phoneno' => $meetings->where('table', 1)->where('slot', 1)->slice(0, 1)->first()->phoneno ?? '',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 1)->where('slot', 1)->slice(1, 1)->first()->name ?? '',
                        'phoneno' => $meetings->where('table', 1)->where('slot', 1)->slice(1, 1)->first()->phoneno ?? '',
                    ],
                ],
                'Slot 2' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 1)->where('slot', 2)->slice(0, 1)->first()->name ?? '',
                        'phoneno' => $meetings->where('table', 1)->where('slot', 2)->slice(0, 1)->first()->phoneno ?? '',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 1)->where('slot', 2)->slice(1, 1)->first()->name ?? '',
                        'phoneno' => $meetings->where('table', 1)->where('slot', 2)->slice(1, 1)->first()->phoneno ?? '',
                    ],
                ],
                'Slot 3' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 1)->where('slot', 3)->slice(0, 1)->first()->name ?? '',
                        'phoneno' => $meetings->where('table', 1)->where('slot', 3)->slice(0, 1)->first()->phoneno ?? '',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 1)->where('slot', 3)->slice(1, 1)->first()->name ?? '',
                        'phoneno' => $meetings->where('table', 1)->where('slot', 3)->slice(1, 1)->first()->phoneno ?? '',
                    ],
                ],
            ],
            'Table 2' => [
                'Slot 1' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 2)->where('slot', 1)->slice(0, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 2)->where('slot', 1)->slice(0, 1)->first()->phoneno ?? ' ',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 2)->where('slot', 1)->slice(1, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 2)->where('slot', 1)->slice(1, 1)->first()->phoneno ?? ' ',
                    ],
                ],
                'Slot 2' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 2)->where('slot', 2)->slice(0, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 2)->where('slot', 2)->slice(0, 1)->first()->phoneno ?? ' ',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 2)->where('slot', 2)->slice(1, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 2)->where('slot', 2)->slice(1, 1)->first()->phoneno ?? ' ',
                    ],
                ],
                'Slot 3' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 2)->where('slot', 3)->slice(0, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 2)->where('slot', 3)->slice(0, 1)->first()->phoneno ?? ' ',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 2)->where('slot', 3)->slice(1, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 2)->where('slot', 3)->slice(1, 1)->first()->phoneno ?? ' ',
                    ],
                ],
            ],
            'Table 3' => [
                'Slot 1' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 3)->where('slot', 1)->slice(0, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 3)->where('slot', 1)->slice(0, 1)->first()->phoneno ?? ' ',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 3)->where('slot', 1)->slice(1, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 3)->where('slot', 1)->slice(1, 1)->first()->phoneno ?? ' ',
                    ],
                ],
                'Slot 2' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 3)->where('slot', 2)->slice(0, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 3)->where('slot', 2)->slice(0, 1)->first()->phoneno ?? ' ',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 3)->where('slot', 2)->slice(1, 1)->first()->name ?? ' ',
                        'phoneno' => $meetings->where('table', 3)->where('slot', 2)->slice(1, 1)->first()->phoneno ?? ' ',
                    ],
                ],
                'Slot 3' => [
                    'participant1' => [
                        'name' => $meetings->where('table', 3)->where('slot', 3)->slice(0, 1)->first()->name ?? '',
                        'phoneno' => $meetings->where('table', 3)->where('slot', 3)->slice(0, 1)->first()->phoneno ?? '',
                    ],
                    'participant2' => [
                        'name' => $meetings->where('table', 3)->where('slot', 3)->slice(1, 1)->first()->name ?? '',
                        'phoneno' => $meetings->where('table', 3)->where('slot', 3)->slice(1, 1)->first()->phoneno ?? '',
                    ],
                ],
            ],
        ];

Then I'll show it in the frontend using tables

I want to transform the meetings table into an array as shown above. What is a better way of doing this? This code is horrible, but I was in a hurry.

I'm open any suggestions, the app is not live yet, so I can change db structure also. Please guide me.

Thanks Ashwin

0 likes
7 replies
LaryAI's avatar
Level 58

To improve the code and make it more readable, you can use loops and arrays to generate the desired structure. Here's an example of how you can refactor the code:

$meetings = Meetings::join('customers', 'customer_id', '=', 'customers.id')
    ->select('meetings.table', 'meetings.slot', 'customers.phoneno', 'customers.name')
    ->get();

$meets = [];

foreach ($meetings as $meeting) {
    $table = 'Table ' . $meeting->table;
    $slot = 'Slot ' . $meeting->slot;
    $participant = [
        'name' => $meeting->name ?? '',
        'phoneno' => $meeting->phoneno ?? '',
    ];

    $meets[$table][$slot]['participant1'] = $participant;
    $meets[$table][$slot]['participant2'] = $participant;
}

This code uses a foreach loop to iterate over each meeting and build the desired array structure. It creates the table and slot keys dynamically based on the meeting's table and slot values. It then assigns the participant information to both 'participant1' and 'participant2' keys.

This approach eliminates the repetitive code and makes it easier to understand and maintain.

1 like
rawatashwin's avatar

@LaryAI Thanks, however I can't use foreach because there is possibility of a few tables and slots being empty. This will prevent me from using the array in the frontend.

krisi_gjika's avatar

something like this?

$meets = [];
foreach($meetings as $meeting) {
  $data = [];
  if (isset($meeting->name, $meeting->phoneno)) {
    $data = ['name' => $meeting->name, 'phoneno' => $meeting->phoneno];
  }

  $meets["Table {$meeting->table}"]["Slot {$meeting->slot}"][] = $data;
}
rawatashwin's avatar

@krisi_gjika Thanks, however I can't use foreach because there is possibility of a few tables and slots being empty. This will prevent me from using the array in the frontend.

krisi_gjika's avatar

@rawatashwin what do you mean by "few tables and slots being empty"? do you want to show these tables in frontend?

rawatashwin's avatar

@krisi_gjika Yes, I want to show all tables and slots so an admin can setup meetings for the empty tables from the frontend.

Please or to participate in this conversation.