Wednesday, February 9, 2022

[FIXED] Why is the value of the 'id' column of the database showing null?

Issue

I am using laravel 6.I have created a table called 'student', where the value increment of the 'id' column is supposed to happen but is not happening.

This is my migration file:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->bigInteger('phone');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
}

In my students table:

enter image description here

My StudentController file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Student;
class StudentController extends Controller
{
   public function student()
   {
        return view('student.create');
   }


   public function index()
   {
        $student = Student::all();
        return view('student.index', compact('student'));
   }

   public function store(Request $request)
   {
     $validatedData = $request->validate([
        'name' => 'required|max:50|min:5',
        'phone' => 'required|unique:students|max:12min:9',
        'email' => 'required|unique:students',
         ]);


    $student = new Student;
    $student->name = $request->name;
    $student->email = $request->email;
    $student->phone = $request->phone;
    $student->save();
    $notification = array(
            'message' => 'Data Insert Done!', 
            'alert-type' => 'success'
            );

            return Redirect()->route('all.category')->with($notification);

    // DB::table('student')->insert($data)
   // return response()->json($student);

   }


   public function ViewStudent()
   {
        
   }

   
}

Model file:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
       protected $fillable = [
        'id','name', 'email', 'phone',
    ];
}


Solution

There is a possibility that you are working with a database whose schema was set for the students table either manually (not through migration, but, for example, by executing an SQL query where auto-increment was not set), or after applying the migration, the auto-increment was removed.

Because your migration code is written correctly according to the official Laravel documentation for the method increments(string $attribute):

enter image description here

I see two solutions here:

  1. change a table column via SQL query so that it matches the description in the migration

ALTER TABLE students CHANGE id id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;

or the same using phpmyadmin or IDE tools;

  1. generate a schema using your migration (php artisan migrate --path=database/migrations/..._create_students_table.php), but for this preliminarily you need to save the students table data, for example, to a dump.

Since you are using phpmyadmin, look at the settings for the id attribute in the students table.



Answered By - Vitaly Vesyolko

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.