I am trying to make two records in two different tables, these are not related, what I want to do is that when I send the information it updates the stock in the store_inventories table, the tables are the following:
This is my method:
public function saveInventoryEntry(Request $request)
{
$message = null;
$data = $request->all();
$ids= [];
try {
foreach ($data as $key => $inventory) {
$inventoryEntry = new StoreInventoryEntry();
$inventoryEntry->inventory_id = $inventory['id'];
$inventoryEntry->code = $inventory['code'];
$inventoryEntry->quantity = $inventory['quantity'];
$inventoryEntry->save();
$ids[] = $inventory['id'];
StoreInventory::whereIn('id',$dt)->update(['stock' => $inventory['quantity']]);
}
$message = $this->sendResponse($inventoryEntry, 'Aggregated data');
} catch (\Exception $e) {
$message = $this->sendError($e->getMessage(), ['Entries could not be registered'], 500);
}
return $message;
}
I send the data as follows:
[
{
"id" : 1,
"code" : "010101",
"quantity" : 10
},
{
"id" : 2,
"code" : "23232",
"quantity" : 7
}
]
Migrations:
public function up()
{
Schema::create('store_inventories', function (Blueprint $table) {
$table->id();
$table->string('code');
$table->string('reference');
$table->string('line');
$table->integer('cost');
$table->integer('stock');
$table->integer('min_stock');
$table->string('location')->default('Almacén');
$table->boolean('status')->default(1);
$table->timestamps();
});
}
public function up()
{
Schema::create('store_inventory_entries', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('inventory_id');
$table->string('code');
$table->integer('quantity');
$table->foreign('inventory_id')->references('id')->on('store_inventories');
$table->timestamps();
});
}
I want to update what I send in quantity to what is in stock in the store_inventories table
source https://stackoverflow.com/questions/70020313/save-records-in-two-tables-in-laravel
No comments:
Post a Comment