How to insert data in laravel 8

How to insert data into database in laravel 8 using Eloquent model


In this tutorial, you will learn how to insert data in laravel 8 using Eloquent model, so before getting started create a new laravel project and give the Database Connection in .env file.

Now, to insert data in laravel 8 into database using Eloquent model. 

Step 1: Create a Model and Migration by following command:

$ php artisan make:model Student -m

Model: Lets open Student Model in following path: app/Model/Student.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;
    protected $table = 'students';
    protected $fillable = [
        'name',
        'email',
        'course',
        'section',
    ];
}

Migration: Lets open create_students_table.php Migration table in following path: database/migrations/2021_05_30_create_students_table.php: (ADD this in your migration table)

public function up()
{
    Schema::create('students'function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email');
        $table->string('course');
        $table->string('section');
        $table->timestamps();
    });
}

Now lets migrate this students table into our database by following command:

$ php artisan migrate

Step 2: Go to web.php file in the following path as: routes/web.php and create a route (url) as follows:

use App\Http\Controllers\StudentController;

Route::get('add-student', [StudentController::class'create']);
Route::post('add-student', [StudentController::class'store']);

Step 3: Create a Controller named StudentController by following command:

$ php artisan make:controller StudentController

after successfully creating the controller lets write the code to View the FORM and insert data from Form in Laravel as follows:

<?php

namespace App\Http\Controllers;

use App\Models\Student;
use Illuminate\Http\Request;

class StudentController extends Controller
{
    public function create()
    {
        return view('student.create');
    }

    public function store(Request $request)
    {
        $student = new Student;
        $student->name = $request->input('name');
        $student->email = $request->input('email');
        $student->course = $request->input('course');
        $student->section = $request->input('section');
        $student->save();
        return redirect()->back()->with('status','Student Added Successfully');
    }
}

Step 4: Create a blade file named create.blade.php in a student folder as show in following the path as:  resource/views/student/create.blade.php

Note: I have used Bootstrap 5 verison to design the User Interface.
@extends('layouts.app')

@section('content')

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6">

            @if (session('status'))
                <h6 class="alert alert-success">{{ session('status'}}</h6>
            @endif

            <div class="card">
                <div class="card-header">
                    <h4>Add Student</h4>
                </div>
                <div class="card-body">

                    <form action="{{ url('add-student') }}" method="POST">
                        @csrf

                        <div class="form-group mb-3">
                            <label for="">Student Name</label>
                            <input type="text" name="name" class="form-control">
                        </div>
                        <div class="form-group mb-3">
                            <label for="">Student Email</label>
                            <input type="text" name="email" class="form-control">
                        </div>
                        <div class="form-group mb-3">
                            <label for="">Student Course</label>
                            <input type="text" name="course" class="form-control">
                        </div>
                        <div class="form-group mb-3">
                            <label for="">Student Section</label>
                            <input type="text" name="section" class="form-control">
                        </div>
                        <div class="form-group mb-3">
                            <button type="submit" class="btn btn-primary">Save Student</button>
                        </div>

                    </form>

                </div>
            </div>
        </div>
    </div>
</div>

@endsection

Step 6: Now we are done with the code of Insert data in laravel. Let's Run the artisan command to start the application as follows:

$ php artisan serve

after servering the artisan - goto: http://localhost:8000/add-student