Answer the question
In order to leave comments, you need to log in
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
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');
});
}
}
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');
}
}
Answer the question
In order to leave comments, you need to log in
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');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question