jordantsap's avatar

multiple days and time foreach

Im building a city guide that one the features is to login and create a company to show it in the frontend, each company has the ability to complete days and hours of that day that the company is open, I have a model company with fields for days (of work) and time foreach of the day that user will choose to complete during company creation, i need to show days and time in the frontend. my current create.blade is: @foreach(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as $day) {{ $day }} Opening Time: Closing Time: @endforeach My controller.store function is:

    $company->days = json_encode($request->input('days'));
    $company->opening_times = json_encode($request->input('opening_times'));
    $company->closing_times = json_encode($request->input('closing_times'));

My migrations are strings: $table->string('days');

And my show view is currently: @foreach(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as $day) <input type="checkbox" name="days[]" value="{{ $day }}" {{ in_array($day, json_decode($company->days)) ? 'checked' : '' }}> {{ $day }}

              <!-- Display opening and closing times for each day -->
              Opening Time: <input type="time" name="opening_times[]" value="{{ $company->opening_time}}">
              Closing Time: <input type="time" name="closing_times[]" value="{{ $company->closing_time}}">

The days that are in the db show as checked but hours dont show at all. days and time in the table are like this: ["Tuesday","Friday","Saturday","Sunday"]. Dont mind to change anything that needs to be changed, iam open to anything

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue seems to be with how the opening and closing times are being displayed in the show view. Currently, the code is trying to access $company->opening_time and $company->closing_time, but these properties do not exist in the $company object. Instead, the opening and closing times are stored as arrays in the opening_times and closing_times properties.

To fix this, you need to loop through the opening_times and closing_times arrays and display the corresponding values for each day. Here's an updated version of the show view code:

@foreach(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as $day)
    <input type="checkbox" name="days[]" value="{{ $day }}" {{ in_array($day, json_decode($company->days)) ? 'checked' : '' }}> {{ $day }}

    <!-- Display opening and closing times for each day -->
    Opening Time: <input type="time" name="opening_times[]" value="{{ $company->opening_times[$loop->index] ?? '' }}">
    Closing Time: <input type="time" name="closing_times[]" value="{{ $company->closing_times[$loop->index] ?? '' }}">

    <br>
@endforeach

This code uses the $loop->index variable to access the corresponding index in the opening_times and closing_times arrays. It also uses the null coalescing operator (??) to handle cases where the array index might not exist.

Make sure to update your code accordingly and test it to see if the opening and closing times are now displayed correctly.

1 like
krisi_gjika's avatar

since each day can have different open - close times, how do you relate your open - close time back to the day after you have stored it?

1 like

Please or to participate in this conversation.