DEV Community

Muhammad zeb Noon
Muhammad zeb Noon

Posted on

How to Transfer Data from PHP Form to Database in Laravel – Easy Steps for Beginners

πŸš€ How to Transfer Data from PHP Form to Database in Laravel – Easy Steps for Beginners

If you're new to Laravel and wondering how to collect form data and store it in a database, you're in the right place! In this tutorial, I’ll guide you through simple, beginner-friendly steps β€” from creating the form to saving the data using Laravel's built-in features.

πŸ› οΈ Step 1: Set Up Laravel Project

If you haven't already installed Laravel, run this:


bash
composer create-project laravel/laravel myFormApp
cd myFormApp
php artisan serve

Make sure your .env file has the correct database credentials:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=form_demo
DB_USERNAME=root
DB_PASSWORD=

Create the database manually in phpMyAdmin or your preferred DB tool.

πŸ“¦ Step 2: Create a Model and Migration
Let’s say we're collecting user contact information.

php artisan make: model Contact -m

Update the migration file in database/migrations:

_

public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->text('message');
$table->timestamps();
});
}
_

Then run the terminal:

> php artisan migrate

πŸŽ‰ Final Result:

Click on http://localhost/phpmyadmin/

  1. Go to the "form_demo" database
  2. Check and click the "Contacts" table created
  3. Check the columns of the Table.

Laravel makes it super easy to create a database with minimal code. Whether you're building a contact form, survey, or registration system database, the process is almost the same.

If you found this helpful, give it a ❀️ and follow for more Laravel beginner tips!


Author: Aurangzeb – alhamrapk.com

Top comments (0)