在 Laravel 中插入关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40468976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:42:42  来源:igfitidea点击:

Inserting with relationships in laravel

phpmysqlsqllaravellaravel-5

提问by secrethash

I am facing a problem in Laravel 5.3 that I looked the docs and also searched web but didn't find anything on it.

我在 Laravel 5.3 中遇到了一个问题,我查看了文档并搜索了网络,但没有找到任何内容。

I am using Laravel Relationships to join two tables. Now I want the data to be inserted on both the tables at the same time after the user submits a form. The catch in this is the first table is the primary one say "users" and second one "xyz" belongsTo the first table. The table "xyz" contains "users_id" column that connects both the tables. And obviously "users_id" is the "id" column of "users" table.

我正在使用 Laravel 关系来连接两个表。现在我希望在用户提交表单后同时将数据插入到两个表中。这里的问题是第一个表是主要的,说“用户”,第二个“xyz”属于第一个表。表“xyz”包含连接两个表的“users_id”列。显然“users_id”是“users”表的“id”列。

Now the problem arriving is that I want to insert the data in "users" table (that is easily done) and "xyz" table at the same time. The User::create()function will create the user data easily and it is working also but for inserting the data in "xyz" table I will be needing the "user_id" column data and ID will not be generated until the user is created as ID column has Auto-Increment attribute activated.

现在出现的问题是我想同时在“users”表(这很容易完成)和“xyz”表中插入数据。该User::create()函数将轻松创建用户数据,它也可以正常工作,但是为了将数据插入“xyz”表中,我将需要“user_id”列数据,并且在创建用户之前不会生成 ID,因为 ID 列具有 Auto-增量属性已激活。

Code:

代码:

    $user = new User;
    $inputArry = array('data1' => $request['field1'],
                    'data2' => $request['field2'],
                    'data3' => $request['field3'],
                    );
    $user->create($inputArry);
    $user->xyz()->create([
        'user_id' => $user->id,
        'name' => $request['name'],
        'about' => $request['desc'],
        'tag' => $request['tag'],
    ]);

Above is the code that I am using for this purpose but it is giving me a error.

以上是我为此目的使用的代码,但它给了我一个错误。

Error:

错误:

    QueryException in Connection.php line 761:
    SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'soft_id' cannot be null (SQL: insert into `xyz` (`user_id`, `name`, `about`, `tag`, `updated_at`, `created_at`) values (, John, I am John, dev, 2016-11-09 21:01:29, 2016-11-09 21:01:29))

回答by Amit Gupta

One way of inserting related table is using relations as:

插入相关表的一种方法是使用关系:

$user = User::create($user_inputs);

$xyz = $user->xyz()->create($xyz_inputs);

It will automatically fills the user_idin the xyztable.

它会自动填充user_idxyz表中。

回答by Ostap B.

If you need insert many items, use createMany or saveMany method.

如果您需要插入多个项目,请使用 createMany 或 saveMany 方法。

For example:

例如:

$post = App\Post::find(1);

$post->comments()->createMany([
    [
        'message' => 'A new comment.',
    ],
    [
        'message' => 'Another new comment.',
    ],
]);

In the offical laravel docs:

在官方 Laravel 文档中:

https://laravel.com/docs/5.6/eloquent-relationships#inserting-and-updating-related-models

https://laravel.com/docs/5.6/eloquent-relationships#inserting-and-updating-related-models

回答by Saumya Rastogi

You can create them like this instead of saving them on same time...

您可以像这样创建它们而不是同时保存它们......

$id = User::create($input_arr)->id;
Xyz::create([
    'user_id' => $id,
    ...
]);