skip to Main Content

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 IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

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 AppHttpControllers;

use IlluminateHttpRequest;
use AppStudent;
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 IlluminateDatabaseEloquentModel;

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

3

Answers


  1. The only reason I can think of is if you did something like this in your model:

        /**
         * Indicates if the IDs are auto-incrementing.
         *
         * @var bool
         */
        public $incrementing = false;
    

    If so then it should be set to true or removed entirely.

    Second, make sure your id is guarded in your model like so:

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];
    

    This way you avoid mass assignments.

    Login or Signup to reply.
  2. Judging by your controller code, I assume the error lies somewhere in the line where you grab an instance of your student model

    Change

     $student = new Student;
    

    To

      $student = new Student();
    

    You need a new instance of a specific model in order to insert a new id, please post your current model code also.

    Sample model code.

    <?php
    
    namespace AppModels;
    
    use IlluminateDatabaseEloquentModel;
    use IlluminateDatabaseEloquentSoftDeletes;
    
    class Product extends Model
    {
        use SoftDeletes;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'product_bar_code', 'product_name', 'product_image', 'product_price'
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
    }
    

    Maybe something is wrong with the way you have written your model code.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search