0

I'm working within Laravel Spark and am inserting values into a MySQL table. I'm performing a series of inserts such as

DB::table('nameOfTable')->insert(['id' =>  $user['id']]);
DB::table('nameOfTable')->insert(['name' =>  $user['name']]);

The values are being inserted into the table properly, but each value is being placed on its own row. There's got to be a simple reason, but I haven't found any luck doing searches. Thanks in advance.

4

1 回答 1

3

您问题中的每一行代码都旨在自行插入一行。为了在同一行中插入多个值(AKA 在同一行中插入不同的列值),您所要做的就是在传递给insert函数的数组中包含更多值:

DB::table('nameOfTable')->insert(['id' =>  $user['id'],'name' =>  $user['name']]);

上一行代码应该可以正常工作。我个人更喜欢这样编写代码,因为它对我们凡人来说更清晰 xD:

DB::table('nameOfTable')->insert(array(
    'id' =>  $user['id'],
    'name' =>  $user['name']
));
于 2016-05-26T23:03:52.160 回答