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

sahar_mkr's avatar

updateOrCreate

hi everyone, I want to check the raws and if there is any match for 3 columns of that raw, just update, but it seems it does not work for my referer part, I wrote something like this: what can I do? I added properties to the fillable properties in my model.

if (!rspTime->isEmpty()) {
            foreach ($rspTime as $key) {
                Cache::updateOrCreate(
                    ['type' => $key->type, 'date' => Carbon::parse($key->date)->format('Y-m-d H:00:00'), 'referer' => $key->referer],
                    [
                        'type' => $key->type,
                        'server_referer' => $key->referer,
                        'response_time' => $key->response_time,
                        'date' => Carbon::parse($key->date)->format('Y-m-d H:00:00'),
                    ]);   
            }
        }
0 likes
14 replies
tykus's avatar

but it seems it does not work for my referer part

What is happening?

sahar_mkr's avatar

@tykus in my table I just see that some parts of the $rspTime are missing, and I can not find them in the table, additionally, I have so much data for the output of $rspTime, but there is just 80 raws in my table, even when I just use create method, this happens too, do you think its because of my huge data?

tykus's avatar

@sahar_mkr you are shaping the data in the query here:

Carbon::parse($key->date)->format('Y-m-d H:00:00')

There a chance that you are effectively filtering the data that is being persisted.

1 like
sahar_mkr's avatar

@tykus actually, first I get the data from another table, with DB::raw, and found the average of the response_time group by every hour, I want to save this data into another table(cache table), every hour of the day.

tykus's avatar

@sahar_mkr ok understood, you have satisfied yourself that the $rspTime Collection has distinct date values?

Why did you determine that the referer was potentially the issue?

sahar_mkr's avatar

@tykus yes, the values per every hour of every day, I thought there may be a problem with the referer when I make dd of $rspTime, I saw all my referees in every hour of that day with the special response_time, but in the table, I just see the data of just one referer has been saved.

Snapey's avatar

you have used both referer and server_referer

Perhaps one is wrong?

Snapey's avatar

@sahar_mkr How come? You use referer as the column name in the query and then server_referer in the data.

You have both columns in your table?

sahar_mkr's avatar

@Snapey there is no problem, I just retrieved data from a table and renamed it with the help of "as", my query was something like this: select server_referer as referer

Snapey's avatar

@sahar_mkr you are not / cannot alias the column in your updateOrCreate

So problem is solved?

1 like
sahar_mkr's avatar

@Snapey I think the problem is with my query, I have so much dirty data, and I need to find a special thing on it, this way When I query from the database and use some calculation like finding the average values directly with SQL, I realized that sql misses some values that I don't know why, so I changed my strategy, now, I am trying to fetch all data first, and then get an average response time per hour by using eloquent roles, and save them in the table one by one, this takes too much time but this way I am sure that I have all my data in the table and I do not have missing data at least. so updateOrCreate works, the problem is with my large data

Snapey's avatar

perhaps start a new question with your actual problem if this code is perfect

Please or to participate in this conversation.