0

I am sending 4 arrays to my controller that they need to combine before I can store them to database, here is how they're look like:

array:4 [▼
  "from" => array:7 [▼
    0 => "8:00"
    1 => null
    2 => null
    3 => null
    4 => null
    5 => null
    6 => null
  ]
  "to" => array:7 [▼
    0 => "21:00"
    1 => null
    2 => null
    3 => null
    4 => null
    5 => null
    6 => null
  ]
  "closed" => array:6 [▼
    0 => "on"
    1 => "on"
    2 => "on"
    3 => "on"
    4 => "on"
    5 => "on"
  ]
  "day_id" => array:7 [▼
    0 => "a0275acb-c6e5-428f-8589-da0644c1f42e"
    1 => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
    2 => "84d74f04-3728-4314-a6e7-8710cecaa911"
    3 => "79ef140a-5de5-4b27-a286-9893cbac820e"
    4 => "6d0b2858-ff96-46ce-ae1b-665c3ee353aa"
    5 => "38225fe6-3bce-4ee9-bad8-c7f6cacc4854"
    6 => "28cef2be-1ff6-4e80-b322-a208f4838391"
  ]
]

As you might notice closed array is less by 1 value as the checkbox was unchecked, so this one is vary between no value (if none are check) to 7 values (if all of them are checked)

Anyway, I need to make this arrays to something like this:

array:7 [▼
  "finaldata" => array:4 [▼
    from => "8:00"
    to => "21:00"
    closed => off
    day_id => "a0275acb-c6e5-428f-8589-da0644c1f42e"
  ],
 "finaldata" => array:4 [▼
    from => null
    to => null
    closed => on
    day_id => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
  ],
  // and so on...
]

If I can get such result then I'll be able to loop and store this data into my database.

Code

$schedules = [];
$schedules['from'] = $request->input('from');
$schedules['to'] = $request->input('to');
$schedules['closed'] = $request->input('closed');
$schedules['day_id'] = $request->input('day_id');
dd($schedules); // returning my results above (on top)

foreach($schedules as $schedule) {
  $days = new WorkDays;
  //...
  $days->save();
}

Question

How can I get such result as I shared to store my data?

Update

blade

<div class="mt-4">
    <div x-data="handler()">
        <x-jet-label value="Schedule" />
        <table class="itable align-middle w-full">
            <thead class="thead-light">
                <tr>
                    <th>Day</th>
                    <th>From</th>
                    <th>To</th>
                    <th>Closed</th>
                </tr>
            </thead>
            <tbody>
                <template x-for="(field, index) in {{$days}}" :key="index">
                    <tr>
                        <td x-text="field.name"></td>
                        <td>
                            <x-jet-input x-model="field.from" type="text" name="from[]" />
                            <x-jet-input x-model="field.id" type="hidden" value="field" name="day_id[]"/>
                        </td>
                        <td><x-jet-input x-model="field.to" type="text" name="to[]" /></td>
                        <td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="closed[]" /></td>
                    </tr>
                </template>
            </tbody>
        </table>
    </div>
    <script>
        function handler() {
            return {
                fields: []
            }
        }
    </script>
</div>

Update 2

based on iCoders answer this is what i get, which is good but the ones with unchecked checkbox will not have closed variable array 0 and 6.

"data" => array:7 [▼
    0 => array:3 [▼
      "from" => "54"
      "day_id" => "a0275acb-c6e5-428f-8589-da0644c1f42e"
      "to" => "474"
    ]
    1 => array:4 [▼
      "from" => null
      "day_id" => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
      "to" => null
      "closed" => "on"
    ]
    2 => array:4 [▼
      "from" => null
      "day_id" => "84d74f04-3728-4314-a6e7-8710cecaa911"
      "to" => null
      "closed" => "on"
    ]
    3 => array:4 [▼
      "from" => null
      "day_id" => "79ef140a-5de5-4b27-a286-9893cbac820e"
      "to" => null
      "closed" => "on"
    ]
    4 => array:4 [▼
      "from" => null
      "day_id" => "6d0b2858-ff96-46ce-ae1b-665c3ee353aa"
      "to" => null
      "closed" => "on"
    ]
    5 => array:4 [▼
      "from" => null
      "day_id" => "38225fe6-3bce-4ee9-bad8-c7f6cacc4854"
      "to" => null
      "closed" => "on"
    ]
    6 => array:3 [▼
      "from" => "67565"
      "day_id" => "28cef2be-1ff6-4e80-b322-a208f4838391"
      "to" => "6567"
    ]
6
  • You need to tie your form controls together so that you know which checkbox is not checked. You can do this by adding an index to your form control's names. I just answered a question like this. stackoverflow.com/questions/64707394/… Commented Nov 6, 2020 at 4:08
  • I think you would have to fix how you send the request data on the frontend. The way you are doing it, you have no way of knowing to which of the registers correspond the elements of the closed array . Commented Nov 6, 2020 at 4:10
  • @porloscerrosΨ I've updated my question would you mind point me what should I do? Commented Nov 6, 2020 at 4:12
  • @bassxzero sorry but I couldn't make relative connection between your answer there and my question here, it didn't help Commented Nov 6, 2020 at 4:13
  • @mafortis.updated answer Commented Nov 6, 2020 at 4:39

2 Answers 2

1

I dont know much usage about new component in laravel 8 but usually common idea is

@foreach($days as $key=>$value)
 <tr>
                        <td x-text="field.name"></td>
                        <td>
                            <x-jet-input x-model="field.from" type="text" name="data[{{$key}}][from]" />
                            <x-jet-input x-model="field.id" type="hidden" value="field" name="data[{{$key}}][day_id]"/>
                        </td>
                        <td><x-jet-input x-model="field.to" type="text" name="data[{{$key}}][to]" /></td>
                        <td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="data[{{$key}}][closed]" /></td>
                    </tr>
                    
                    @endforeach

Then in controller if you dd($request->data) you get merged data

Updated

As per another question answer Dynamically set name attribute of input field in loop with AlpineJS You need to use x-bind:name with a template string:

 <template x-for="(field, index) in {{$days}}" :key="index">
                    <tr>
                        <td x-text="field.name"></td>
                        <td>
                            <x-jet-input x-model="field.from" type="text" x-bind:name="`data[${index}][from]`/>
                            <x-jet-input x-model="field.id" type="hidden" value="field" x-bind:name="`data[${index}][day_id]`"/>
                        </td>
                        <td><x-jet-input x-model="field.to" type="text" x-bind:name="`data[${index}][to]` /></td>
                        <td><input x-model="field.closed" class="form-checkbox" type="checkbox" x-bind:name="`data[${index}][closed]` /></td>
                    </tr>
                </template>

Update2:

<input x-model="field.closed" class="form-checkbox" type="checkbox" name="data[{{$key}}][closed]" @click="$event.target.value = $event.target.checked ? '1' : '2'"/>
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you it return results very good but, it has small issue I will update my question so you can see it\
oaky will check
still no have closed 0 => array:3 [▼ "from" => "325" "day_id" => "a0275acb-c6e5-428f-8589-da0644c1f42e" "to" => "352" ]
@mafortisif checkbox is not clicked then key wont available in laravel.you may need to add validation for that
How do I add that?
|
0

Change your blade to something like this.

<template x-for="(field, index) in {{$days}}" :key="index">
    <tr>
        <td x-text="field.name"></td>
        <td>
            <x-jet-input x-model="field.from" type="text" name="finaldata[{{$loop->iteration}}]['from']" />
            <x-jet-input x-model="field.id" type="hidden" value="field" name="finaldata[{{$loop->iteration}}]['day_id']"/>
        </td>
        <td><x-jet-input x-model="field.to" type="text" name="finaldata[{{$loop->iteration}}]['to']" /></td>
        <td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="finaldata[{{$loop->iteration}}]['closed']" /></td>
    </tr>
</template>

5 Comments

Undefined variable: loop
Wait is that x-for loop directive for your JS framework?
it's laravel 8 uses alpine js
Yeah you're looping in your JS framework so this won't work. How do you get the index number you are on in your for loop?
days in in my table are dynamic (which I only loop them, the rest are static as user fills) so the index is based on days array. (field, index) in {{$days}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.