如何在Laravel 5中创建表迁移

4

我正在开发一个项目,需要为应用程序创建用户管理。但是我卡在了表关系和迁移方面。

尝试

我有以下这些表:

  1. 用户
    • 用户ID
    • 用户名
    • 密码
  2. 档案
    • 档案ID
    • 用户ID
    • 名字
    • 姓氏
    • 电子邮件
  3. 地址
    • 地址ID
    • 档案ID
    • 地址
    • 城市
    • 国家
    • 邮编
  4. 配置
    • 配置ID
    • 配置名称
    • 配置类型
    • 父ID

现在我需要为上述结构创建模型和迁移。为此,我已经创建/修改了以下模型和迁移类。

模型:用户

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function profile()
    {
        return $this->hasOne('Profile','user_id');
    }
}

迁移: 2014_10_12_000000_create_users_table.php


该迁移文件用于创建用户表。
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('user_id');
            $table->string('username');
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

模型:档案

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    public function user(){
        return $this->belongsTo('User');
    }
    public function address()
    {
        return $this->hasOne('Address','address_id');
    }
}

迁移:2016_02_26_101749_create_profiles_table.php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('profile_id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
            $table->string('lastname')->nullable();
            $table->string('firstname')->nullable();
            $table->string('gender')->nullable();
            $table->string('email')->unique();
            $table->string('phonenumber', 20)->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('profiles');
    }
}

模型:地址

namespace App;

use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    public function profile(){
        return $this->belongsTo('Profile');
    }

    public function city() {
        return $this->hasOne('Configuration', 'config_id');
    }

    public function state() {
      return $this->hasOne('Configuration', 'config_id');
    }

    public function country() {
        return $this->hasOne('Configuration', 'config_id');
    }
}

迁移:2016_02_26_102805_create_addresses_table.php

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

class CreateAddressesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('addresses', function (Blueprint $table) {
            $table->increments('address_id');
            $table->integer('profile_id')->unsigned();
            $table->foreign('profile_id')->references('profile_id')->on('profiles')->onDelete('cascade');
            $table->string('address')->nullable();
            $table->integer('city')->unsigned();
            $table->foreign('city')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->string('pincode')->nullable();
            $table->integer('state')->unsigned();
            $table->foreign('state')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->integer('country')->unsigned();
            $table->foreign('country')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('addresses');
    }
}

模型:配置

namespace App;

use Illuminate\Database\Eloquent\Model;

class Configuration extends Model
{
    public function children() {
        return $this->hasMany('Configuration','parent_id');
    }
    public function parent() {
        return $this->belongsTo('Configuration','parent_id');
    }
   public function address(){
        return $this->belongsTo('Address');
    }
}

迁移:2016_02_26_104519_create_configurations_table.php

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

class CreateConfigurationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('configurations', function (Blueprint $table) {
            $table->increments('config_id');
            $table->string('configuration_name');
            $table->string('configuration_type');
            $table->string('parent_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('configurations');
    }
}

现在,当我运行php artisan migrate时,我收到以下错误消息:migration error
请告诉我如何解决这个问题。我必须使用相同的表结构,不能修改它。如果需要进一步更新或我忘记了什么,请让我知道。

我在迁移表中创建了user_id而不是id,所以我认为这不是问题所在。我认为问题出在地址和配置上,因为地址包含城市、州、国家列,这些列将是来自配置表的外键。 - Mandy
是的,抱歉我发表评论后才意识到你在“用户”表中的列名为“user_id”,这让我有点困惑。我已经删除了该评论。 - DavidT
单独运行迁移以查看哪个出错。 - Alexey Mezenin
好的,我会尝试。我已经尝试过了,但是我没有找到可以为每个表运行单独迁移的内容。请告诉我如何为每个模型单独运行迁移。 - Mandy
1个回答

2

这是因为迁移会在配置之前尝试迁移地址表,因此找不到你所引用的外键config_id,所以你可以更改迁移文件的名称,然后migration命令将首先传递configurations_table迁移文件,然后是addresses_table迁移文件,所以只需更改:

2016_02_26_104519_create_configurations_table.php

致:

2016_02_26_102005_create_configurations_table.php
_____________^

接下来,您应该运行优化命令以重新生成优化的类加载器:

php artisan o

现在重新运行php artisan migrate命令,问题应该得到解决。
希望这有所帮助。

1
是的,它解决了我的问题。您能告诉我我的模型和它们之间的关系是否正确吗? - Mandy
第一次看到同一列(config_id)代表不同的信息(城市,州,国家),不确定它是否有效。 - Zakaria Acharki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接