【问题标题】:Why is the value of the 'id' column of the database showing null?为什么数据库的“id”列的值显示为空?
【发布时间】:2020-12-18 10:53:48
【问题描述】:

我正在使用 laravel 6。我创建了一个名为 'student' 的表,其中 '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');
    }
}

在我的学生表中:

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

【问题讨论】:

  • 你似乎在id 中插入了一个空值,如果你正在使用$students-&gt;id = $someVar 之类的实现,请将其删除
  • @Ainz-sama 先生,在我的实现中,我不使用 like$students-&gt;id = $someVar 这个表达式。
  • 通过SHOW CREATE TABLE students;检查你的表结构是id列有AUTO_INCREMENT
  • 显示你的控制器
  • 只需从可填充中取出‘id‘,它就会按预期工作。

标签: php laravel eloquent phpmyadmin laravel-6


【解决方案1】:

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

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

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

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

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) 生成架构,但首先您需要将学生表数据保存到转储中。

由于您使用的是phpmyadmin,请查看students 表中id 属性的设置。

【讨论】:

  • @HumayunAhmadRajib,所以您可以接受我的回答作为最佳答案。
【解决方案2】:

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

    /**
     * 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'];

这样可以避免大量分配。

【讨论】:

  • 你应该只在你没有使用明确的可填充列表的地方保护它,只使用一个或另一个 - 但不能同时使用
  • 我们不确定他是如何填写他的数据的,所以排除了明显的可能性。
  • 你没有错,我只是说如果模型已经用可填充列表声明了,则不应使用受保护的 ;-)
  • @Qirel 先生,我正在使用可填充。
  • @Humayun 快速提问,希望您没有在可填写内容中包含id?如果你的回答是否定的,那么我建议更新到最新版本的 Laravel 6,或者如果可能的话迁移到 7。
【解决方案3】:

从您的控制器代码来看,我认为错误出现在您获取学生模型实例的行中

改变

 $student = new Student;

  $student = new Student();

您需要特定模型的新实例才能插入新 id,请同时发布您当前的模型代码。

示例模型代码。

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

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

【讨论】:

  • 问题已解决,谢谢大佬帮助我。
  • new Student;new Student(); 相同
猜你喜欢
  • 2017-04-02
  • 2019-10-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2016-05-14
  • 2021-07-26
相关资源
最近更新 更多