skip to Main Content

I am working on a Laravel project with PhpMyAdmin and I am quite new to Laravel, I have created tables with models, and already migrated them, now I need to add new columns but when I add the columns and then migrate it shows that noting there to be migrated

Also same thin for the relationships, I applied 1-1 between 2 tables and migrated, but the database is not updated, and from time to time I got an error of duplicating session table as well, how to solve this?

Students table

<?php
    
    use IlluminateDatabaseMigrationsMigration;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateSupportFacadesSchema;
    
    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');
        }
    };

Projects table

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

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

Project model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

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

    }
}

Student model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class student extends Model
{
    use HasFactory;

    public function getProject(){
        return $this->hasOne('Appproject');

    }
}

2

Answers


  1. In Laravel, we don’t add/modify columns in migrated files. We use separate migration for it. Or else you can use

    php artisan migrate:fresh
    

    This will drop all your data and rebuild the table migration. But creating separate migration is recommended.


    and the relationship namespace is wrong

    return $this->belongsTo('Appproject');
    

    should be

    return $this->hasOne('AppModelsProject');
    
    Login or Signup to reply.
  2. Once you have migrated the table there are 2 options to add new columns/new conditions/ new modifications.

     1- Create a new migration & associate it with the existing one.
     2- Run command:     php artisan migrate:fresh
    

    it will drop all your tables and then again migrate them and the changes reflect.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search