π 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:
php artisan migrate
check
http://localhost/phpmyadmin/
Top comments (0)