Search code examples
phpmysqllaravellaravel-spark

Inserts into MySQL table going onto separate rows


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.


Solution

  • Each line of code you have in your question was designed to insert a row by itself. In order to insert more than one value in the same row (AKA insert various column values in the same row), all you have to do is include more values inside the array that is being passed to the insert function:

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

    The previous line of code should work fine. I personally prefer to write the code as follows since it is cleaner to read for us mortals xD:

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