Home > Software design >  Save records in two tables in laravel?
Save records in two tables in laravel?

Time:11-19

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:

enter image description here

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

CodePudding user response:

I'm not sure your migration files are as it should be, it seems you could make it better, however what you're looking for is Eloquent Relationships

        foreach ($data as $item) {
            $inventory = StoreInventory::findOrFail($item['id']);
            $inventory->stock = $item['quantity'];
            $inventory->save();
            $inventory->entries()->create([
                'quantity' => $item['quantity'],
            ]);            
        }

Note: you have to add entries relationship to your StoreInventory model.

CodePudding user response:

I solved it as follows:

public function saveInventoryEntry(Request $request)
{

    $request->validate(['*.inventory_id' => 'required|numeric', '*.quantity' => 'required|numeric']);

    $message = null;               
    $stock = [];
    $result = [];
    $inventory_entry_id = [];
    $data = $request->all();
    
    try {
        //I go through the data that I receive
        foreach ($data as $key => $inventory) {                
            $inventoryEntry = new StoreInventoryEntry();
            $inventoryEntry->inventory_id = $inventory['inventory_id'];                
            $inventoryEntry->quantity = $inventory['quantity'];
            $inventoryEntry->save();

            $inventory_entry_id[] = $inventory['inventory_id'];
            $inventory_entry[] = $inventory['quantity'];                
            $result[] = $inventoryEntry;
            
            //I store the stock
            $dataInventoryById = StoreInventory::select('stock')->whereIn('id', $inventory_entry_id)->get();                                               
        }  
        
        //I go through the stock and then update it
        foreach ($dataInventoryById as $key => $value) {
            $stock[] = $value->stock $inventory_entry[$key];
            StoreInventory::where('id', $inventory_entry_id[$key])->update(['stock' => $stock[$key]]);
        } 
        
        $message = $this->sendResponse($result,'Added Inventory Entries');

    } catch (\Exception $e) {
        $message = $this->sendError($e->getMessage(), ['The entries could not be added'], 500);
    }

    return $message;
}
  • Related