17

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.

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.

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.

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))
secrethash
  • 182
  • 1
  • 1
  • 12
  • What exactly is the question? This is pretty standard DB stuff; you can't create a child record without first saving the parent. `$user = User::create(...);` has to be called before you try to save the information in table `xyz`. – Tim Lewis Nov 07 '16 at 15:41
  • Use "last insert id" from users-table/insert query in the second query as an user_id http://stackoverflow.com/questions/27873777/how-to-get-last-insert-id-in-eloquent-orm-laravel `$user = User::create($data); $insertedId = $user->id;` – P_95 Nov 07 '16 at 15:42
  • Yeah, I called `$user = User:: create([…]);` then after that how can I add the saved user's id to "xyz" table's "user_id"? – secrethash Nov 07 '16 at 15:49

4 Answers4

24

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_id in the xyz table.

Amit Gupta
  • 17,072
  • 4
  • 41
  • 53
7

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

For example:

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

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

In the offical laravel docs:

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

Ostap Brehin
  • 3,240
  • 3
  • 25
  • 28
-1

Be long relationship

$account = App\Account::find(10);
 
$user->account()->associate($account);
 
$user->save();
-2

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

$id = User::create($input_arr)->id;
Xyz::create([
    'user_id' => $id,
    ...
]);
Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45