Home > Mobile >  Saving Array ID to Database with value to each id
Saving Array ID to Database with value to each id

Time:02-23

Good Evening.... Hope i can explain my problem correctly. I am getting data (ID) in array and value (numbers) in controller. Now i want to save the "numbers" in each "ID".

       array ID ["Buffalo-01", "Buffalo-02", "Buffalo-04"] 
       Numbers - 40.

Want to save 40 to each ID.

Controller

    public function addbuffalototalmilk(Request $req )
        {
            $buffalomilking         =   Buffalodata::where('avgmilk','<>','0')->Where('status','=','Available')->count(); // MIlking Animal Nos
            $getbuffalomilkingid    =   Buffalodata::where('avgmilk','<>','0')->Where('status','=','Available')->pluck('buffaloID'); // Get Buffalo Details of Milking

            $totalmorningmilk       =   $req->get('morningtotalmilk');
            $totaleveningmilk       =   $req->get('eveningtotalmilk');

            $eachmorningmilk        =   ($totalmorningmilk / $buffalomilking);
            $eacheveningmilk        =   ($totaleveningmilk / $buffalomilking);

        
            
            
            return response ();
        } 

Thanks in Advance

CodePudding user response:

Is this what you are looking for ?

$buffalomilking = Buffalodata::where('avgmilk', '<>', '0')
    ->Where('status', '=', 'Available')
    ->count();

$getbuffalomilkingid = Buffalodata::where('avgmilk', '<>', '0')
    ->Where('status', '=', 'Available')
    ->pluck('buffaloID');

foreach ($getbuffalomilkingid as $id) {
    Buffalodata::where('id', $id)->update([
        'number' => $buffalomilking,
    ]);
}
  • Related