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

MahmoudAdelAli's avatar

Add addtional value to created_at when send request

hello again , i have a invoice table , there's timestamps inside , my idea is form to edit invoice data but when the user change the date just the date the created at will be updated with the date selected by the user and the current time - i know it's bad idea but the client needed :D - now i make a StoreRequest with the following code

<?php

namespace App\Http\Requests\Admin\Invoice;

use App\Helpers\Classess\CanAccess;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;

class StoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return CanAccess::permission('can_moderate_invoices');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        return [
            "name" => ['required', 'min:3', 'max:255'],
            "description" => ['required', 'min:3', 'max:255'],
            "recipient" => ['required', 'min:3', 'max:255'],
            "amount" => ['required','numeric','digits_between:1,20'],
            "type" => ['required','numeric','digits_between:1,2'],
            'created_at' => ['required','date_format:Y-m-d H:i:s']
        ];
    }
    protected function prepareForValidation()
    {
        $this->merge([
            'created_at' => date('Y-m-d H:i:s', strtotime($this->created_at.Carbon::now()->format('H:i:s')))
        ]);
    }
}

it work very good but i think it not the best practice cause we have a code review and the team leader give me a warning for thad

0 likes
12 replies
Snapey's avatar

I would be more concerned about writing the validated data directly into the model (I assume thats what you are doing)

I assume you need the created_at to be validated because you are mass assigning?

1 like
MahmoudAdelAli's avatar

@Snapey yes i use mass assigning but the data not in model direct it's inside a Request and i have edited the code , and i use the request inside the controller if there's any thing wrong in this way I will be grateful if you tell me

MahmoudAdelAli's avatar

@Snapey i use it like that :D

public function store(StoreRequest $request)
    {
        Invoice::create($request->validated());
        return redirect()->back()->with('success',__('messages.has_created'));
    }
iftekhs's avatar

How about you create a brand new field called custom_date and set that to the time that the user submits with. So you will also have the correct created_at timestamp which you can use if you need it at any point.

Snapey's avatar

You can remove the prepare for validation, and the validation rule, and then change the controller;

public function store(StoreRequest $request)
    {
        Invoice::create($request->validated() + ['created_at' => now());
        return redirect()->back()->with('success',__('messages.has_created'));
    }
Snapey's avatar

Hang on, you are doing a create not an update.

You don't need to do anything. created_at will be automatically set to the current time !

1 like
Snapey's avatar

I re-read your question.

OK, so what is the field name of the date input and what format date does the form send ?

1 like
MahmoudAdelAli's avatar

@Snapey i put the incorrect code for update the code is

   public function update(UpdateRequest $request, Invoice $invoice)
    {

        $invoice->update($request->validated());
        return redirect()->back()->with('success', __('messages.has_updated'));
    }

the specific part here is when the user change the invoice date the created_at will updated but the time will be the current

Snapey's avatar

@MahmoudAdelAli I try again

OK, so what is the field name of the date input and what format date does the form send ?

1 like
MahmoudAdelAli's avatar

@Snapey the form send a date and his name is 'created_at' and in request i merge the time with him and send him with the user date he was choose and the current time

Please or to participate in this conversation.