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

debiprasad's avatar

Recommended place in Laravel to alter form input

In Laravel, what is the recommended place to alter the form input before storing it in the database?

For example, I want to convert a string received as input to lowercase letters before I store it in the database.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The recommended place to alter form input before storing it in the database is in the controller's store method. You can use Laravel's built-in validation and then modify the input before saving it to the database. Here's an example:

public function store(Request $request)
{
    $validatedData = $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:8|confirmed',
    ]);

    // Modify the input before saving it to the database
    $validatedData['name'] = strtolower($validatedData['name']);

    $user = User::create($validatedData);

    return redirect('/home');
}

In this example, we're using Laravel's validation to validate the input. After validation, we're modifying the name input to lowercase before saving it to the database.

Please or to participate in this conversation.