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

emilpapelas4@gmail.com's avatar

Cant store Api data into db.

I trying to store some Api data into db but receive error.

ErrorException Undefined index: name

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

     $offers = $offers['data'];

    //dd($offers);

     Offers::create([

        'name' => $offers['name'],

        'description' => $offers['description'],

        'payout' => $offers['revenue'],

        'amount' => $offers['currencyReward'],

        'link' => $offers['link'],

        'image' => $offers['image'],

        'countries' => 'Ro',

        'devices' => 'Android',

        'count' => 1,

     ]);
0 likes
23 replies
jlrdw's avatar

@emilpapelas4@gmail.com

While you loop, put data in an array like:

$data = [
    ['some_key'=>'Bob', 'some_number'=> 4096],
    ['some_key'=>'Harry', 'some_number'=> 2048],
    //...
];

then

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

That's it.

tykus's avatar

@emilpapelas4@gmail.com $offers is an Array after this assignment:

$offers = $offers['data'];

so you need to iterate over the Array (added some fault tolerance using null coalescing operator too)

foreach($offers as $offer) {
     Offers::create([
        'name' => $offer['name'] ?? '',
        'description' => $offer['description'] ?? '',
        'payout' => $offer['revenue'] ?? 0,
        'amount' => $offer['currencyReward'] ?? 0,
        'link' => $offer['link'] ?? '',
        'image' => $offer['image'] ?? '',
        'countries' => 'Ro',
        'devices' => 'Android',
        'count' => 1,
     ]);
}

This is super inefficient however - it will make one query to your DB for every item in the Array (which appears to be approx. 3000 items.

Consider chunking the Array and performing multiple inserts in a single query

emilpapelas4@gmail.com's avatar

@tykus may this work but get an error for payout column that is a decimal but some time can be a text like Variable because the payout in some case be variable or a decimal.

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect decimal value: 'variable' for column wall.offers.payout at row 1 (SQL: insert into offers (name, description, payout, amount, link, image, countries, devices, count, updated_at, created_at) values (BitLabs Surveys, Complete a survey. Can be completed multiple times per day., variable, variable, https://api.lootably.com/api/offerwall/redirect/offer/26-1?placementID=cki5rbmik000j01z29404h5je&rawPublisherUserID={userID}, https://api.lootably.com/api/offerwall/image/26-1, Ro, Android, 1, 2022-04-21 17:34:05, 2022-04-21 17:34:05))

tykus's avatar

@emilpapelas4@gmail.com in that case you need to check the value coming from the API, and pass an appropriate alternative value to your table; for example, substitute variable string for 0

foreach($offers as $offer) {
     Offers::create([
        'name' => $offer['name'] ?? '',
        'description' => $offer['description'] ?? '',
        // decimal columns
        'payout' => is_numeric($offer['revenue'] ?? 0) ?: 0,
        'amount' => is_numeric($offer['currencyReward'] ?? 0) ?: 0,

        'link' => $offer['link'] ?? '',
        'image' => $offer['image'] ?? '',
        'countries' => 'Ro',
        'devices' => 'Android',
        'count' => 1,
     ]);
}

I don't know what you want an an appropriate substitute whenever you receive the string variable; but you need to pass a number or (if the column is nullable) null

emilpapelas4@gmail.com's avatar

@tykus also forget the countries and devices is an array of items in this case how can i store them beucase if use

$offer['devices'] ?? '',

will not work

tykus's avatar

@emilpapelas4@gmail.com you can json_encode the array providing your column can handle the length

devices' => json_encode($offer['devices'] ?? []),
emilpapelas4@gmail.com's avatar

@tykus ok good i have some private questions for you if u are able to talk with me on skype or messenge i will appreciate it.

emilpapelas4@gmail.com's avatar

@tykus ok no problem

there is 1 more issue

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'countries' at row 1 (SQL: insert into offers (name, description, payout, amount, link, image, countries, devices, count, updated_at, created_at) values (UNIVERSE, Install and open., 1, 1, https://api.lootably.com/api/offerwall/redirect/offer/10-2571301?placementID=cki5rbmik000j01z29404h5je&rawPublisherUserID={userID}, https://api.lootably.com/api/offerwall/image/10-2571301, ["AL","DZ","AO","AR","AM","AT","AZ","BS","BH","BY","BZ","BJ","BO","BA","BW","BR","BG","BF","KH","CM","CV","CL","CO","CR","CI","HR","CZ","DK","DO","EC","EG","SV","EE","FJ","FI","FR","GA","GH","GR","GT","GN","HN","HK","HU","IS","IN","ID","IQ","IE","IL","IT","JM","JO","KZ","KE","KW","KG","LA","LV","LB","LT","LU","MY","ML","MU","MX","MD","MA","MZ","NA","NP","NZ","NI","NE","NG","NO","OM","PK","PA","PG","PY","PE","PH","PL","PT","QA","RO","RU","RW","SA","SN","SK","SI","ZA","ES","LK","SE","CH","TJ","TZ","TH","TT","TN","TR","TM","UG","UA","AE","US","UY","UZ","VE","VN","YE","ZM","ZW","AG","BM","CS","KY","MK","MM","MO","MT","TC","VG"], ["android"], 1, 2022-04-21 18:21:40, 2022-04-21 18:21:40))

the countries data some time will to long ,

tykus's avatar
tykus
Best Answer
Level 104

@emilpapelas4@gmail.com like I said...

you can json_encode the array providing your column can handle the length

Your column cannot handle data that length! You will need to either (i) alter your table to make countries column a TEXT or LONGTEXT type; (ii) truncate the data before inserting (which throws away information you might need) or (iii) extract a separate pivot table for offer_country and store the data that way instead

Please or to participate in this conversation.