【问题标题】:3 Table Laravel Migration need advice3 表 Laravel 迁移需要建议
【发布时间】:2021-12-23 04:03:59
【问题描述】:

我认为我这样做是正确的。我有一个用户、个人资料和应用程序表。以下是迁移。我认为我在迁移中正确链接了它们,但需要建议以确保。 1 个用户可以拥有 1 个个人资料,但可以拥有多个应用程序。

用户表

        public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
            });
        }
    
        

个人资料表

       public function up()
    {
        Schema::create('profile', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->unsigned();
            $table->string('address')->nullable();
            $table->string('apt')->nullable();
            $table->string('city')->nullable();
            $table->string('state')->nullable();
            $table->string('zipcode')->nullable();
            $table->string('homephone')->nullable();
            $table->string('mobile')->nullable();
            $table->string('occupation')->nullable();
            $table->boolean('over18')->nullable();
            $table->string('homechurch')->nullable();
            $table->string('homechurchcity')->nullable();
            $table->string('pastor')->nullable();
            $table->string('howoftenattend')->nullable();
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    

应用程序

    public function up()
    {
        Schema::create('pilgrim_application', function (Blueprint $table) {
            $table->id();
            $table->string('besttimetocall');
            $table->string('nickname');
            
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('profile_id')->constrained('profile')->onDelete('cascade');

            $table->timestamps();
        });
   

【问题讨论】:

    标签: laravel migration relationship


    【解决方案1】:

    您的迁移目前是否有效?

    首先检查你的迁移文件顺序,例如

    0000_user

    0001_profile

    1000_pilgrim_application

    1001_app_two

    如果有多个应用程序,您可以使用不同的编号开始迁移,以便在列表中对文件进行分组,如果您想创建多个应用程序,这会很方便。

    另外,是否需要在 pilgrim_application 迁移中指定关系用户和配置文件?

    通过模型中的关系,您还可以从用户模型访问 pilgrim_application,反之亦然。

    $user->profile->pilgrim_application
    

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 2015-07-17
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 2020-12-13
      • 2013-09-26
      相关资源
      最近更新 更多