π 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/
- Go to the "form_demo" database
- Check and click the "Contacts" table created
- 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!
Top comments (0)