Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

emilpapelas4@gmail.com's avatar

Need help storing data from foreach into db.

I trying to store data from an api link that foreach -ed to get all data needed and store but will store only 1 data.

0 likes
27 replies
Snapey's avatar

You think we are mind readers?

1 like
emilpapelas4@gmail.com's avatar

@Snapey

$data= Http::get('https://api.lootably.com/api/v1/offers/get?&apiKey=d7culg61xyw1i6ucg99edipd8iyxjuect20dx662d9&placementID=cki5rbmik000j01z29404h5je&rawPublisherUserID='.$session_id)->json();

    $data = $data['data'];

    $userid = $user;
    foreach($data as $offer)
    {
      $offers = Offers::orderBy('count', 'desc')->first();

      $name = $offer['name'];
      $desc = $offer['description'];
      $prevenue = $offer['revenue'];
      $amount = $offer['currencyReward'];
      //$country = $offer['countries'];
      //$devices = $offer['devices'];
      $img = $offer['image'];

      $url = str_replace("{userID}" , $userid , $offer['link']);

        if(!$offers){
            Offers::create([
                'name' => $name,
                'description' => $desc,
                'payout' => $prevenue,
                'amount' => $amount,
                'link' => $url,
                'image' => $img,
                'countries' => "RO",
                'devices' => "Android",
                'count' => 1,
            ]);
        }
        else{
            $offers->update([
                'count' => $offers->count + 1,
            ]);
        }

       
    }
frankielee's avatar

@emilpapelas4@gmail.com

ok but can u tell me if that was the issue because only 1 Data is stored in dB but API have 2900 data.

yes, that is the issue.

BTW, do you know what the codes do?

 $offers = Offers::orderBy('count', 'desc')->first();
jlrdw's avatar

@emilpapelas4@gmail.com can't you just make a data array:

$data = array();
//start foreach
$data[] = // add to data array the rows while doing the foreach

Then

DB::table('your_table')->insert($data);

Of course check if empty and things like that. Also strip_tags if needed, etc...

emilpapelas4@gmail.com's avatar

@jlrdw Maybe like this ? This still not works for me

$data= Http::get('https://api.lootably.com/api/v1/offers/get?&apiKey=d7culg61xyw1i6ucg99edipd8iyxjuect20dx662d9&placementID=cki5rbmik000j01z29404h5je&rawPublisherUserID='.$session_id)->json();

    $data = $data['data'];
    //dd($data);
    $userid = $user;

    
       $data = array();
    foreach($data as $offer)
    {
     
 $data[] = 
      $name = $offer['name'];
      $desc = $offer['description'];
      $prevenue = $offer['revenue'];
      $amount = $offer['currencyReward'];
      //$country = $offer['countries'];
     // $devices = $offer['devices'];
      $img = $offer['image'];
      $url = str_replace("{userID}" , $userid , $offer['link']);

      if(!$offers){
           Offers::create([
                'name' => $name,
                'description' => $desc,
                'payout' => $prevenue,
                'amount' => $amount,
                'link' => $url,
                'image' => $img,
                'countries' => "RO",
                'devices' => "Android",
                'count' => 0,
           ]);
        }
        DB::table('offers')->insert($data);

    }
Snapey's avatar

what your code says and what you say are completely different

you say you want to store 2900 records, but you get one record and update its count

my guess is that this line

$offers = Offers::orderBy('count', 'desc')->first();

is missing some where statement that would match the record in your table to the one from the api

but as you cannot describe what the code should do it's impossible to help

emilpapelas4@gmail.com's avatar

@Snapey I try to store all data from api to my db , maybe i dont wirte the code correctly . if this was the issue

$offers = Offers::orderBy('count', 'desc')->first();

then how can i fix it?

JohnnyBigodes's avatar

Have you created the table? Is the id column of your table auto increment?

Other than that it seems nobody understands what you are trying to achieve with your code.

Please or to participate in this conversation.