Set a fixed value for a field in a multi-row insert in Laravel

Samuel Shifterovich

I have an array $cart:

$cart = [
    ["product_id" => 1, "price" => 20, "amount" => 5, "tax" => 15],
    ["product_id" => 2, "price" => 30, "amount" => 10, "tax" => 20],
    ...
];

I can do

DB::table('order_products')->insert($cart);

But I need to insert this array into a table that has the same columns + order_id. The order_id will be the same for all rows, it's the insert id of the previous query. Is it possible to set a fixed value for order_id for all rows?

Paul Spiegel

When you use eloquent models and your Order model has a hasMany relation order_products(), then you can use the createMany() method on the relation:

$order->order_products()->createMany($cart);

For save() and saveMany() methods as well as for create() and createMany(), if called on a relation, the foreign key (in your case order_id) will be set automatically taken from the parent $order entity.

In the documentation you will also find this note:

Before using the create method, be sure to review the documentation on attribute mass assignment.

Most important is the $fillable property, which you already figured out.

Note that you should also be able to use create() for the order itself. I don't know why it didn't work for you. Check the $fillable property in the Order model. Also usually an order belongs to a user - so you would need to assign user_id, which might be missing in the array. What could work is

$order = Auth:user()->orders()->create($orderArray);

The user_id would be taken from Auth:user() and assigned to the order automatically.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Set the value of an input field

From Dev

Multiple insert based on value of a field?

From Dev

python mysql multi-row insert fail

From Dev

jQuery: how to set fixed decimals for input field value

From Dev

Set field value with reflection

From Dev

Insert dropdown value into text field

From Dev

To copy a row and insert in same table with a one different value for a field in MySQL

From Dev

mongoose compound index for fixed value field

From Dev

Laravel get relation count where a field in the database is set to a certain value

From Dev

Multi-row insert with pg-promise

From Dev

How to insert a field after check the value of a field

From Dev

set a default value for select field in Laravel using collective form builder?

From Dev

awk to insert value and remove field

From Dev

ORA-12704: character set mismatch when performing multi-row INSERT of nullable NVARCHAR's

From Dev

mysql On Duplicate value in field, insert new row with new value

From Dev

Just insert a new key - value on a mysql json field on Laravel

From Dev

awk to insert value and remove field

From Dev

Insert fixed a value in a joined table

From Dev

Insert nVarchar value into Bigint field

From Dev

Set an entire field row value if equal to a single value

From Dev

Sitecore set multi reference field value programmatically

From Dev

how to set value in text field in angularjs on row click

From Dev

insert rows and increase a value in a field

From Dev

Insert VALUES from query multi row

From Dev

Mysql set different UUID for different row in multi row insert

From Dev

How to insert a field after check the value of a field

From Dev

postgresql: INSERT INTO … (SELECT * …,"fixed value")

From Dev

How to insert array field in laravel

From Dev

kotlin field is not set the value

Related Related

  1. 1

    Set the value of an input field

  2. 2

    Multiple insert based on value of a field?

  3. 3

    python mysql multi-row insert fail

  4. 4

    jQuery: how to set fixed decimals for input field value

  5. 5

    Set field value with reflection

  6. 6

    Insert dropdown value into text field

  7. 7

    To copy a row and insert in same table with a one different value for a field in MySQL

  8. 8

    mongoose compound index for fixed value field

  9. 9

    Laravel get relation count where a field in the database is set to a certain value

  10. 10

    Multi-row insert with pg-promise

  11. 11

    How to insert a field after check the value of a field

  12. 12

    set a default value for select field in Laravel using collective form builder?

  13. 13

    awk to insert value and remove field

  14. 14

    ORA-12704: character set mismatch when performing multi-row INSERT of nullable NVARCHAR's

  15. 15

    mysql On Duplicate value in field, insert new row with new value

  16. 16

    Just insert a new key - value on a mysql json field on Laravel

  17. 17

    awk to insert value and remove field

  18. 18

    Insert fixed a value in a joined table

  19. 19

    Insert nVarchar value into Bigint field

  20. 20

    Set an entire field row value if equal to a single value

  21. 21

    Sitecore set multi reference field value programmatically

  22. 22

    how to set value in text field in angularjs on row click

  23. 23

    insert rows and increase a value in a field

  24. 24

    Insert VALUES from query multi row

  25. 25

    Mysql set different UUID for different row in multi row insert

  26. 26

    How to insert a field after check the value of a field

  27. 27

    postgresql: INSERT INTO … (SELECT * …,"fixed value")

  28. 28

    How to insert array field in laravel

  29. 29

    kotlin field is not set the value

HotTag

Archive