Anonymous194's avatar

Laravel $request->all(), replace one input with another

Hi, I have a form which submits various data. Most of them are normal inputs with a string, however weekdays contains checkboxes which are an array. I.e

                  <div class="form-field" name="weekdays" id="weekday">
                    <input class="weekday form-field" type="checkbox" name="weekdays[]" value="MO">Monday
                    <input class="weekday form-field" type="checkbox" name="weekdays[]" value="TU">Tuesday
                    <input class="weekday form-field" type="checkbox" name="weekdays[]" value="WE">Wednesday
                    <input class="weekday form-field" type="checkbox" name="weekdays[]" value="TH">Thursday
                    <input class="weekday form-field" type="checkbox" name="weekdays[]" value="FR">Friday
                    <input class="weekday form-field" type="checkbox" name="weekdays[]" value="SA">Saturday
                    <input class="weekday form-field" type="checkbox" name="weekdays[]" value="SU">Sunday
                  </div>

Which give me:


"weekdays" => array:2 [    0 => "TH"    1 => "FR"  ]

I have tried:

        $weekday_string = implode(",", $request->input('weekdays'));
        $request->merge(array('weekdays', $weekday_string));

However that gives me:

0 => "weekdays"  1 => "TH,FR"]

But in my database I need it like this:


 "weekdays" => "TH,FR"

Here's my Controller:

    $test = ($request->input('weekdays'));
        $weekday_string = implode(",", $request->input('weekdays'));
        $request->merge(array('weekdays', $weekday_string));
        dd($request->all());
        $event = DirtyEvent::create($request->all());

How can I achieve it?

0 likes
3 replies
36864's avatar
$weekday_string = implode(",", $request->input('weekdays'));
$request->merge(array('weekdays', $weekday_string));

Try replacing that with

$weekday_string = implode(",", $request->input('weekdays'));
$request->merge(array('weekdays' => $weekday_string));
1 like
sanderwind's avatar
Level 2

Did not test this, but change this;

$request->merge(array('weekdays', $weekday_string));

to

$request->merge(array('weekdays' => $weekday_string));
1 like

Please or to participate in this conversation.