Laravel 5如何在创建时向用户添加多个外键约束? [英] Laravel 5 How to add multiple foreign key constraints to a User on creation?

查看:336
本文介绍了Laravel 5如何在创建时向用户添加多个外键约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在知道可以通过执行以下操作来使用区域和用户之间的haveMany关系创建用户:

I now know that I can create a User using the haveMany relationship between a Region and User by doing this:

$region = Region::find($data['region_id']);

$region->users()->create([
   'username' => $data['username'],
   'email' => $data['email'],
   'first_name' => $data['first_name'],
   'last_name' => $data['last_name'],
   'password' => bcrypt($data['password']),
]);

但是,如果有多个外键,该如何创建一个用户而不使它们可填充多个外键约束呢?如果有3或4怎么办?

But what if there are multiple foreign keys, how do you create a user without making them fillable for more than 1 foreign key constraint? What if there were 3 or 4?

在不同模型中建立关系的示例:

Example of the relationships setup in the different models:

Region hasMany Users, or
User belongsTo a Region

Location hasMany Users, or 
User belongsTo a Location

如果我的迁移的简化版本有助于更好地理解关系,则如下所示:

A stripped down version of my migration looks like this if it helps to understand the relationships better:

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('region_id')->unsigned();
        $table->integer('location_id')->unsigned();
        $table->timestamps();

        $table->foreign('region_id')
              ->references('id')
              ->on('regions');

        $table->foreign('location_id')
              ->references('id')
              ->on('locations');
    });

    Schema::create('regions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 100);
        $table->timestamps();
    });

    Schema::create('locations', function (Blueprint $table) {
        $table->increments('id');
        $table->string('street_address', 100);
        $table->string('city', 50);
        $table->string('province', 50);
        $table->string('country', 50);
        $table->string('postal_code', 10);
        $table->timestamps();
    });

区域和位置模型示例:

/**
 * Region may have many users.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function users()
{
    return $this->hasMany('App\User');
}


/**
 * Location may have many users.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function users()
{
    return $this->hasMany('App\Location');
}

推荐答案

我假设您有一个用于Region和Location的模型.您的每个区域都有一个ID,位置有一个ID,然后只需添加

I assume you have a model for Region and Location. Each of your region has an id and location has an id, then just add

$this->hasMany('where your Region model is', 'region_id', 'id');
$this->hasMany('where your Location model is', 'location_id', 'id');

您的用户模型

参考: http://laravel.com/docs/5.0/eloquent#一对多

您也可以在任何模型中进行

You can also do in any of your model

if ($this->create()) {
  return (new Model())->create(the params you need in another model);
}

这篇关于Laravel 5如何在创建时向用户添加多个外键约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆