Upserts seeming to fail silently
I am tasked with pulling data from one database and store it in another. I was attempting to batch upsert these records in groups of 100, I would always get to 19,000 records saved and the Queue and Jobs would just stop. No errors, no failed Jobs, nothing in the laravel.log
, nada. I had a pretty good feeling it had to do with some resource limits being hit somewhere. I just wasn't sure at what level; PHP, MySQL, OS?
This was happening due to an out of memory error but it was not being reported by Laravel. My hunch is this was happening because if memory limit is reached, the worker just stops before anything can get logged.
The $visible property
When you add the $visible
property to a Model, this does not prevent the columns that are not defined in this array from being queried and returned. This property just prevents data from those column from being displayed or
In my case, I was trying to really limit the data being returned because we only needed a handful of columns from the 145 columns that exist in that table (This is not a database we commissioned or maintain). Pulling in all these additional columns was causing the memory issue. The model is question was being pulled via a relationship so my solution was to add a select
to the relationship which lessened the data being queried.
public function relationship(): HasOne
{
return $this
->hasOne(Relationship::class)
->select(['name', 'title', 'slug', 'date']);
}
Other notes
You must specify an orderBy clause when using this function.