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:
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
The only reason I can think of is if you did something like this in your model:
If so then it should be set to true or removed entirely.
Second, make sure your
id
is guarded in your model like so:This way you avoid mass assignments.
Judging by your controller code, I assume the error lies somewhere in the line where you grab an instance of your student model
Change
To
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.
Maybe something is wrong with the way you have written your model code.
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)
:I see two solutions here:
ALTER TABLE students CHANGE id id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY;
or the same using phpmyadmin or IDE tools;
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 thestudents
table.