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

CodyMcCode's avatar

Adding extra information to a validated form request data to create a model

Hello,

I want to include a filed 'joined_at' which is the date at which a user joined our platform (and because it's been offline previously and people have join dates this is not 'created_at'. I could just allow this to be null for now but I'd like to prefil it with the sign up date (effectively 'created_at') i.e. now. Then edit particular users later.

My question is: what is the best way to "inject" date into a handling of a request before I create a new model database entry based on the request i.e.

public function store(Request $request){
    $attributes = $request->validate([
        'nickname' => ['required'],
        'first_name' => ['required'],
        'last_name' => ['required'],
        'email' => ['required', 'email', 'max:254'],
        'password' => ['required', Password::min(5)->numbers()->mixedCase(), 'confirmed']
    ]);
    //Here I want to assign the value 'joined_at' to be the current timestamp.
    $user = User::create(
        $attributes
    );

I know this is a basic question but I couldn't find an answer (probably because I don't know what to search!).

0 likes
4 replies
LaryAI's avatar
Level 58

To include the joined_at field with the current timestamp in your form request data before creating a new model, you can simply add the joined_at field to the $attributes array after validation. Here's how you can do it:

public function store(Request $request)
{
    // Validate the request data
    $attributes = $request->validate([
        'nickname' => ['required'],
        'first_name' => ['required'],
        'last_name' => ['required'],
        'email' => ['required', 'email', 'max:254'],
        'password' => ['required', Password::min(5)->numbers()->mixedCase(), 'confirmed']
    ]);

    // Add the 'joined_at' field with the current timestamp
    $attributes['joined_at'] = now();

    // Create the user with the attributes
    $user = User::create($attributes);

    // Optionally, you can return a response or redirect
    return response()->json($user, 201);
}

In this solution:

  1. The request data is validated first.
  2. The joined_at field is added to the $attributes array with the current timestamp using now().
  3. The User model is created with the updated $attributes array.

This way, the joined_at field will be automatically populated with the current timestamp when a new user is created.

CodyMcCode's avatar

Ok, Larry has potentially answered this immediately that I just append the timestamp like so:

// Add the 'joined_at' field with the current timestamp $attributes['joined_at'] = now();

This seems correct but would be great if it can be confirmed... Why am I overthinking this?

tykus's avatar

If you are using a FormRequest class, then you can use the safe attributes and merge additional data:

// StoreUserRequest
public function rules(): array
{
    return [
        'nickname' => ['required'],
        'first_name' => ['required'],
        'last_name' => ['required'],
        'email' => ['required', 'email', 'max:254'],
        'password' => ['required', Password::min(5)->numbers()->mixedCase(), 'confirmed']
    ];
}
// user Controller
public function store(StoreUserRequest $request)
{
    $user = User::create($request->safe()->merge(['joined_at' => now()]);
    return redirect()//...

There are a number of other approaches that you could take to achieve the same result; this is just closest to your original attempt.

Please or to participate in this conversation.