R
R
Radiss2019-04-28 21:16:19
Laravel
Radiss, 2019-04-28 21:16:19

Why is the new field not being added to the table?

Laravel 5.2 I
have two related tables: Users (genus) and Countries of type hasOne
Now I want to implement a hasMany - Users relationship to Articles Created a
migration to add a foreign key to articles
add_articles_user_table

spoiler
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddArticlesUserTable extends Migration
{
    public function up()
    {
        Schema::table('articles', function (Blueprint $table) {
            $table->integer('user_id')->unsigned()->default(1);
            $table->foreign('user_id')->references('id')->on('users');
        });
    }

    public function down()
    {
        Schema::table('articles', function (Blueprint $table) {
            $table->dropColumn('user_id');
        });
    }
}


n.
create_articles_table migration
spoiler
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration
{
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {           
            $table->increments('id');
            $table->string('name',100);//Varchar 100
            $table->text('text');//text
            $table->string('img',255);//Varchar 100
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('articles');
    }
}

5cc5eddcb962d705605399.jpeg
But interestingly, another migration from another project (a copy but with a different date) worked, why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
segio_tt, 2019-04-29
@radiss

AddArticlesUserTable or just AddArticlesTable
Read the documentation, judging by this entry, articles_user (I would call article_users) is added to the table, and accordingly
Schema::table('articles_user', function (Blueprint $table) {
$table->integer('user_id') ->unsigned()->default(1);
$table->foreign('user_id')->references('id')->on('users');
});

P
php_raper, 2019-04-29
@php_raper

Good afternoon!
It is written that the table already exists, and without your migration files, it’s not exactly clear what you wrote wrong.
Hypothetically you don't check

if (!Schema::hasTable('articles')) {
 //create code here
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question