Home > Mobile >  Why new columns are not migrated, and relationships are not applied in Laravel?
Why new columns are not migrated, and relationships are not applied in Laravel?

Time:12-29

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

Projects table

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

Project model

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

    }
}

Student model

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

    }
}

CodePudding user response:

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.

CodePudding user response:

Welcome to laravel & stack,

You cannot simply do things directly into the migration file, once it has been executed, it will be marked as completed in the database migration table, what you want to do instead is (example):

Step 1) Create a new migration file:

php artisan make:migration add_paid_to_users_table --table=users

Step 2) Go to the newly created migration file and you can add a column like this, including dropping:

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('paid');
    });
}

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('paid');
    });
}

Step 3) Migrate your new migration

php artisan migrate
  • Related