Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phayes0289's avatar

Creating a New Date from Three Parts. to Update A Database Feild

I have a form in that I want to create a date from three fields. The first "date" field comes from a JavaScript plugin. The next is a simple hour field with a choice of 0 through 23. The next is a "minutes" field with choices of 00,10,20,30,40,50.

The code in my controller appears to work. If I do a dd($formFields) just before the update, the new value for pub_date shows as the date I created from the three parts.:

"pub_date" => "2022-08-26 04:00"

To be clear, I am attempting to overwrite the value of the original form field, which contains only a date and replace it with my newly created date and time.

When I run the update from the blade, it updates all the other fields but just skips the "pub_date" field. What am I missing?

This is my controller.

public function update(Request $request, pibs $pibs)
    {
        $formFields = $request->validate([
        'addr_number' => ['required'],
        'addr_street'  => ['required'],
        'addr_city'  => ['required'],
        'addr_state'  => ['required'],
        'addr_postalcode'  => ['required'],
        'addr_lat'  => ['nullable'],
        'addr_lng'  => ['nullable'],
        'tags'  => ['nullable'],
        'pub_date'  => ['nullable']

        ]);
        
        if ($request->pub_date != null ){
        $combined_date_and_time = $request->pub_date . ' ' . $request->pub_hour . ':' . $request->pub_minute . ':00';
        $new_pub_date = Carbon::parse($combined_date_and_time);
        $formFields['pub_date'] = $new_pub_date->format('Y-m-d h:i');
        }
    
        $pibs->update($formFields);
        $pibs->save();

        return back()->with('message', 'Building Survey updated successfully!');
    }
0 likes
3 replies
martinbean's avatar
Level 80

@phayes0289 You’d be better off building a date fluently, rather than doing all that string manipulation.

if ($request->has('pub_date')) {
    $publishedDate = $request->date('pub_date');
    $publishedDate->setHour($request->input('pub_hour'));
    $publishedDate->setMinute($request->input('pub_minute'));

    $request->merge([
        'pub_date' => $publishedDate,
    ]);
}

$pibs->update($request->validated());

You also don’t need to call save after update.

phayes0289's avatar

@martinbean I see your point regarding the string manipulation. I rewrote it as you suggested.

I have never seen the request->merge before. But I understand it now.

But where does it get the "Validated()" from? Do I have to rewrite the validation code?

Please or to participate in this conversation.