You think we are mind readers?
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.
$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,
]);
}
}
@emilpapelas4@gmail.com describe what you think this code is doing?
@Snapey first I want to get all data from api , then store into bd.
$offers = Offers::orderBy('count', 'desc')->first();
Fix your logic. You are going to get the same Offers and updating the same row only..
@frankielee any example ?
Ref: https://laravel.com/docs/9.x/queries#where-clauses
Filter by using the unique value from any column?
@frankielee may replace with ID?
I don't know man.
You are the one involved in the project. You should able to decide that.
@frankielee ok but can u tell me if that was the issue bexause only 1 Data stored in dB but api have 2900 data.
@emilpapelas4@gmail.com i dont see how anyone can advise how to fix the code when we don't know what it is supposed to do
@Snapey Alreadt say that want to get all datas from that api link and store into db.
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();
@frankielee first will get data with foreach then , store in db?
No.
$offers = Offers::orderBy('count', 'desc')->first();
The query generated will be
Select * from `offers` order by `count` desc limit 1
You are going to get only the first item that has the highest count.
Edit: I think you need this. https://laracasts.com/series/laravel-8-from-scratch
@frankielee then how to create the query to work ?
Please help me.
@frankielee Still cannot fix this please help.
@emilpapelas4@gmail.com No more questions on this forum until you have seen the whole series:
@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...
@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);
}
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
@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?
@emilpapelas4@gmail.com don't just keep repeating the same answer
We need to know how a record in the api is matched to a record in the database
@emilpapelas4@gmail.com I suggest follow @michaloravec comment and take a little more training. That course that was linked is free. But just my suggestion.
@emilpapelas4@gmail.com how could that possibly help?
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.