1

I am trying to store Stat and End time for each Day. These Select elements refer to 'Start Time' and 'Ending Time' I am able to get the values but I am not sure how to store and retrieve them in my view. I am storing them by combining both arrays with the days separately and then json_encode but I am not able to retrieve them.

Store Blade View

<div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="days[]" value="Monday" id="flexCheckDefault">
                                            <label class="form-check-label" for="flexCheckDefault">
                                                Monday
                                            </label>

                                            <div class="reveal-if-active">
                                                <div class="row">
                                                    <div class="col-md-4">
                                                        <label for="which-dog">From</label>
                                                        <select name="from[]" class="form-control">
                                                            <option value="1">01:00</option>
                                                            <option value="2">02:00</option>
                                                            <option value="3">03:00</option>
                                                            <option value="4">04:00</option>
                                                            <option value="5">05:00</option>
                                                            <option value="6">06:00</option>
                                                            <option value="7">07:00</option>
                                                            <option value="8">08:00</option>
                                                            <option value="9">09:00</option>
                                                            <option value="10">10:00</option>
                                                            <option value="11">11:00</option>
                                                            <option value="12">12:00</option>
                                                            <option value="13">13:00</option>
                                                            <option value="14">14:00</option>
                                                            <option value="15">15:00</option>
                                                            <option value="16">16:00</option>
                                                            <option value="17">17:00</option>
                                                            <option value="18">18:00</option>
                                                            <option value="19">19:00</option>
                                                            <option value="20">20:00</option>
                                                            <option value="21">21:00</option>
                                                            <option value="22">22:00</option>
                                                            <option value="23">23:00</option>
                                                            <option value="24">24:00</option>
                                                        </select>
                                                    </div>
                                                    <div class="col-md-4">
                                                        <label for="which-dog">To</label>
                                                        <select name="to[]" class="form-control">
                                                            <option value="1">01:00</option>
                                                            <option value="2">02:00</option>
                                                            <option value="3">03:00</option>
                                                            <option value="4">04:00</option>
                                                            <option value="5">05:00</option>
                                                            <option value="6">06:00</option>
                                                            <option value="7">07:00</option>
                                                            <option value="8">08:00</option>
                                                            <option value="9">09:00</option>
                                                            <option value="10">10:00</option>
                                                            <option value="11">11:00</option>
                                                            <option value="12">12:00</option>
                                                            <option value="13">13:00</option>
                                                            <option value="14">14:00</option>
                                                            <option value="15">15:00</option>
                                                            <option value="16">16:00</option>
                                                            <option value="17">17:00</option>
                                                            <option value="18">18:00</option>
                                                            <option value="19">19:00</option>
                                                            <option value="20">20:00</option>
                                                            <option value="21">21:00</option>
                                                            <option value="22">22:00</option>
                                                            <option value="23">23:00</option>
                                                            <option value="24">24:00</option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="days[]" value="Tuesday" id="flexCheckChecked">
                                            <label class="form-check-label" for="flexCheckChecked">
                                                Tuesday
                                            </label>

                                            <div class="reveal-if-active">
                                                <div class="row">
                                                    <div class="col-md-4">
                                                        <label for="which-dog">From</label>
                                                        <select name="from[]" class="form-control">
                                                            <option value="1">01:00</option>
                                                            <option value="2">02:00</option>
                                                            <option value="3">03:00</option>
                                                            <option value="4">04:00</option>
                                                            <option value="5">05:00</option>
                                                            <option value="6">06:00</option>
                                                            <option value="7">07:00</option>
                                                            <option value="8">08:00</option>
                                                            <option value="9">09:00</option>
                                                            <option value="10">10:00</option>
                                                            <option value="11">11:00</option>
                                                            <option value="12">12:00</option>
                                                            <option value="13">13:00</option>
                                                            <option value="14">14:00</option>
                                                            <option value="15">15:00</option>
                                                            <option value="16">16:00</option>
                                                            <option value="17">17:00</option>
                                                            <option value="18">18:00</option>
                                                            <option value="19">19:00</option>
                                                            <option value="20">20:00</option>
                                                            <option value="21">21:00</option>
                                                            <option value="22">22:00</option>
                                                            <option value="23">23:00</option>
                                                            <option value="24">24:00</option>
                                                        </select>
                                                    </div>
                                                    <div class="col-md-3">
                                                        <label for="which-dog">To</label>
                                                        <select name="to[]" class="form-control">
                                                            <option value="1">01:00</option>
                                                            <option value="2">02:00</option>
                                                            <option value="3">03:00</option>
                                                            <option value="4">04:00</option>
                                                            <option value="5">05:00</option>
                                                            <option value="6">06:00</option>
                                                            <option value="7">07:00</option>
                                                            <option value="8">08:00</option>
                                                            <option value="9">09:00</option>
                                                            <option value="10">10:00</option>
                                                            <option value="11">11:00</option>
                                                            <option value="12">12:00</option>
                                                            <option value="13">13:00</option>
                                                            <option value="14">14:00</option>
                                                            <option value="15">15:00</option>
                                                            <option value="16">16:00</option>
                                                            <option value="17">17:00</option>
                                                            <option value="18">18:00</option>
                                                            <option value="19">19:00</option>
                                                            <option value="20">20:00</option>
                                                            <option value="21">21:00</option>
                                                            <option value="22">22:00</option>
                                                            <option value="23">23:00</option>
                                                            <option value="24">24:00</option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

controller

**ADD FUNCTION**

$from = json_encode(array_combine($request->days, $request->from));
$to = json_encode(array_combine($request->days, $request->to));

$business->hrs_from = $from;
$business->hrs_to = $to;

**EDIT FUNCTION**

dd((json_decode($businesses[0]->hrs_from)));

Response From Dump Die

{#1130 ▼
  +"Monday": "1"
  +"Tuesday": "1"
  +"Thursday": "1"
  +"Wednesday": "1"
}
6
  • I would consider saving them in a column each and not combining them, what is the benefit of you doing this? Commented Apr 26, 2021 at 20:21
  • the user will store the starting and closing time for open day of their business. and a user can have multiple businesses. Commented Apr 26, 2021 at 20:23
  • if I save them in separate columns then there might be a problem with retrieving the data for his multiple busniess. we may need a separate table for it then? Commented Apr 26, 2021 at 20:25
  • Yes if that is the case, I would create a table which has business_id, day, start, end. Did you also think about including breaks? LIke for lunch? And how are you saving holidays or special closed days? Commented Apr 26, 2021 at 20:27
  • Yes, it will have business_id and maybe user_id too? I am not including the breaks because according to requirements I only need start and end time from the user. and Yes I am saving the holidays and special days with separate checkboxes by exploding the array and then imploding it Commented Apr 26, 2021 at 20:31

2 Answers 2

2

Add second parameter to json_decode to get array

dd((json_decode($businesses[0]->hrs_from, true)));
Sign up to request clarification or add additional context in comments.

Comments

1

You can use nested arrays in html form input names. For example:

from[day][]
from[time][]

or even:

from[day][0][]
from[time][0][]
from[day][1][]
from[time][1][]

It should arrive correctly on the backend. Not sure if this helps?

I have found it especially useful with php code that has a lot of data and especially in tables.

5 Comments

Thank you for it. so should I use this name in the Days input or in the Starting time select input?
hmm you might need to use variables for the name. so for the day it would be from[$dayValue] and for the start time it would be from[$dayValue][$startValue]. Likely need some javascript to update the values when one of the dropdowns changes
from[time][monday][] I put the day like this and it is giving the day at the key and two arrays as starting time and end time. how do you suggest I should store it? I mean should I do json_encode or store it as text/implode.
and then what would be the best way to retrieve it? to the blade
i usually serialize the whole form with this approach

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.