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