为什么数据库的'id'列的值显示为null?

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

我正在使用 laravel 6.I 创建了一个名为 'student' 的 table,其中 'id' 列的值增量应该发生但是没有发生。

这是我的迁移文件:

<?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');
    }
}

在我的学生中table:

我的 StudentController 文件:

<?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()
   {
        
   }

   
}

模型文件:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

我能想到的唯一原因是如果你在你的模型中做了这样的事情:

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

如果是这样,则应将其设置为 true 或完全删除。

其次,确保你的 id 在你的模型中受到保护,如下所示:

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

这样可以避免批量分配。

根据您的控制器代码判断,我认为错误出在您获取学生模型实例的那一行中

改变

 $student = new Student;

  $student = new Student();

您需要特定型号的新实例才能插入新 ID,请 post 您当前的型号代码。

示例模型代码。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

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'];
}

可能您编写模型代码的方式有问题。

您可能正在使用一个数据库,其架构是手动设置的 students table(不是通过迁移,而是例如,通过执行 SQL 未设置 auto-increment 的查询),或者在应用迁移后,删除了 auto-increment。

因为你的迁移代码是根据方法official Laravel documentation正确编写的 increments(string $attribute):

我在这里看到两个解决方案:

  1. 通过 SQL 查询更改 table 列,使其与迁移中的描述相匹配

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

或使用 phpmyadmin 或 IDE 工具相同;

  1. 使用您的迁移生成一个模式 (php artisan migrate --path=database/migrations/..._create_students_table.php),但是为此您首先需要将学生 table 数据保存到转储中。

由于您使用的是 phpmyadmin,请查看 students table.[=18= 中 id 属性的设置]