If the only data changing in this scenario is a nested Price record, I would consider an endpoint/Route/Controller to handle that. Otherwise in your example, you are potentially going to need to interrogate the Material instance and each Price record to know what has been updated.
Mar 16, 2021
2
Level 10
How to update a collection with a request that has full collection as array
Hi, Sorry if my title is a bit confusing. I have a Material.php record
SKU: "DFAS"
company_id: 2
created_at: "2021-03-16T03:33:54.000000Z"
id: 6
name: "Almond"
prices: Array(12)
0: {id: 37, material_id: 6, price: "4.45", month: "2021-04-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
1: {id: 38, material_id: 6, price: "4.45", month: "2021-05-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
2: {id: 39, material_id: 6, price: "4.45", month: "2021-06-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
3: {id: 40, material_id: 6, price: "4.45", month: "2021-07-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
4: {id: 41, material_id: 6, price: "4.45", month: "2021-08-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
5: {id: 42, material_id: 6, price: "4.45", month: "2021-09-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
6: {id: 43, material_id: 6, price: "4.45", month: "2021-10-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
7: {id: 44, material_id: 6, price: "4.45", month: "2021-11-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
8: {id: 45, material_id: 6, price: "4.45", month: "2021-12-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
9: {id: 46, material_id: 6, price: "4.45", month: "2022-01-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
10: {id: 47, material_id: 6, price: "4.45", month: "2022-02-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
11: {id: 48, material_id: 6, price: "4.45", month: "2022-03-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
length: 12
__proto__: Array(0)
supplier_id: 10
type: "ingredient"
updated_at: "2021-03-16T03:33:54.000000Z"
user_id: 2
and in vue I change one of the prices of one of the months to 4.30 and then I do an axios call with the updated record
SKU: "DFAS"
company_id: 2
created_at: "2021-03-16T03:33:54.000000Z"
id: 6
name: "Almond"
pivot: {recipe_id: 2, material_id: 6}
prices: Array(12)
0: {id: 37, material_id: 6, price: "4.30", month: "2021-04-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
1: {id: 38, material_id: 6, price: "4.45", month: "2021-05-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
2: {id: 39, material_id: 6, price: "4.45", month: "2021-06-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
3: {id: 40, material_id: 6, price: "4.45", month: "2021-07-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
4: {id: 41, material_id: 6, price: "4.45", month: "2021-08-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
5: {id: 42, material_id: 6, price: "4.45", month: "2021-09-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
6: {id: 43, material_id: 6, price: "4.45", month: "2021-10-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
7: {id: 44, material_id: 6, price: "4.45", month: "2021-11-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
8: {id: 45, material_id: 6, price: "4.45", month: "2021-12-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
9: {id: 46, material_id: 6, price: "4.45", month: "2022-01-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
10: {id: 47, material_id: 6, price: "4.45", month: "2022-02-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
11: {id: 48, material_id: 6, price: "4.45", month: "2022-03-01 00:00:00", created_at: "2021-03-16T03:33:54.000000Z", …}
length: 12
__proto__: Array(0)
supplier_id: 10
type: "ingredient"
updated_at: "2021-03-16T03:33:54.000000Z"
user_id: 2
Is there an easy way in my controller to replace all the entire record? Something like
return $material->load('prices')->update($request)
This gives me the error Model::update() must be of the type array, object given
Level 104
Please or to participate in this conversation.