-1

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

    }
}
Shadow
  • 33,525
  • 10
  • 51
  • 64
Amida
  • 23
  • 6
  • 2
    Don't change the already migrated migration files, but create new ones and apply those changes there, then you have something to migrate. Alternatively you can run: `php artisan migrate:fresh` which will undo all migrations and re-migrate with your edited files – UnderDog Dec 29 '22 at 06:12
  • I still face the same thing `SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ` I tried almost all the methods, that I read but it migrates some tables and then suddenly stops at one of them – Amida Dec 30 '22 at 06:23

2 Answers2

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.

  • I still face the same thing `SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ` I tried almost all the methods, that I read but it migrates some tables and then suddenly stops at one of them – Amida Dec 30 '22 at 06:23
  • This shows that you have already made the column in that migration or you don't dropped that column in the down method of the migration. Take a review at that. – Ahtasham Yousaf Jan 11 '23 at 12:10
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('App\project');

should be

return $this->hasOne('App\Models\Project');
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • I still face the same thing `SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name ` I tried almost all the methods, that I read but it migrates some tables and then suddenly stops at one of them – Amida Dec 30 '22 at 06:23