phpmyadmin 为什么在Laravel中不迁移新列,也不应用关系?

7rtdyuoh  于 2022-12-29  发布在  PHP
关注(0)|答案(1)|浏览(126)

我正在使用PhpMyAdmin处理一个Laravel项目,我对Laravel很陌生,我已经创建了带有模型的表,并且已经迁移了它们,现在我需要添加新列,但是当我添加列然后迁移时,它显示要迁移的注解
对于关系也是一样的,我在2个表之间应用了1-1并进行了迁移,但数据库没有更新,而且我还不时地收到复制会话表的错误,如何解决这个问题?
学生桌

<?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('students', function (Blueprint $table) {
                $table->id();
                $table->string(column:"Name");
                $table->string(column:"Semester");
                $table->unsignedBigInteger('project_id')->nullable(false);
                $table->foreign('project_id')->references('id')->on('projects');
                $table->timestamps();
            });
    
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('students');
        }
    };

项目表

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->id();
            $table->string("Title");
            $table->date("Start Date");
            $table->date("End Date");
            $table->date("Type");
            $table->integer("Duration");
            //$table->unsignedBigInteger("lecturer_id");
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('projects');
    }
};

项目模式

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class project extends Model
{
    use HasFactory;
    public function getStudnet(){
        return $this->belongsTo('App\project');

    }
}

学生模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class student extends Model
{
    use HasFactory;

    public function getProject(){
        return $this->hasOne('App\project');

    }
}
nhaq1z21

nhaq1z211#

在Laravel中,我们不会在迁移的文件中添加/修改列。我们使用单独的迁移。或者您可以使用

php artisan migrate:fresh

这将删除所有数据并重建表迁移。但建议创建单独的迁移
和关系命名空间错误

return $this->belongsTo('App\project');

应该是

return $this->hasOne('App\Models\Project');

相关问题