亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么數據庫的“id”列的值顯示為空?

為什么數據庫的“id”列的值顯示為空?

PHP
弒天下 2023-11-05 15:53:37
我正在使用 laravel 6。我創建了一個名為 的表'student',其中列的值增量'id'應該發生,但沒有發生。這是我的遷移文件:<?phpuse 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');    }}在我的學生表中:
查看完整描述

3 回答

?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

您所使用的數據庫有可能是students手動(不是通過遷移,而是通過執行未設置自動增量的 SQL 查詢)或在應用遷移之后為表設置了架構的數據庫,自動增量已被刪除。

因為您的遷移代碼是根據該方法的官方 Laravel 文檔increments(string $attribute)正確編寫的:

https://img1.sycdn.imooc.com/6559d69800019d0f05000221.jpgz

我在這里看到兩個解決方案:

  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,但為此,您首先需要保存學生表數據,例如保存到轉儲。

由于您使用的是phpmyadminid?,請查看表中屬性的設置students。


查看完整回答
反對 回復 2023-11-05
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

根據您的控制器代碼判斷,我認為錯誤位于您獲取學生模型實例的行中的某個位置


改變


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

}

也許您編寫模型代碼的方式有問題。


查看完整回答
反對 回復 2023-11-05
?
慕斯王

TA貢獻1864條經驗 獲得超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'];

這樣您就可以避免大量分配。


查看完整回答
反對 回復 2023-11-05
  • 3 回答
  • 0 關注
  • 229 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號