How to process data arrays with adding to the database?

Viewed 35

Help logically understand, I do not quite understand how you can implement this moment. There is a form:

@foreach($weeks as $key => $value)
  <li>
       <label>
           <input type="checkbox" value="{{$key}}" name="days[]"> {{$value}}
            <div class="checkbox"></div>
       </label>
       <div class="time" data-day="mon">
             <input type="time" name="start_time[]" class="start">
              <span>-</span>
              <input type="time" name="end_time[]" class="finish">
     </div>
</li>
@endforeach

enter image description here

Images are attached to this form. My relationship is many to many. In the request I get the following data:

enter image description here

images go to one table, days and times to another, intermediate between them. The problem is, I can't catch up with how to add data to the table with days. Since I get 3 arrays - days, start time and end. I parsed the request in the controller. I add data to a table with an image.

$data = $request->validated();
$days[] = $data['days'];
$days[] = $data['start_time'];
$days[] = $data['end_time'];
unset($data['days'], $data['start_time'], $data['end_time']);

$image = Storage::disk('public')->put('/images', $data['image']);
$cover = DynamicCover::create([
        'image' => $image,
        'group_link' => $data['group_link'],
    ]);

The following data remains in the days array:

enter image description here

And here I am at a dead end, I don’t understand how to process them correctly, add them to the database and also fill in the link table I will be grateful for advice. Many-to-many relationships, because there can be many images for one day, so this image will have many days

0 Answers
Related