Try this:
$limite = 10;
$claims = Refunds::query()
->select(['claims_id'])
->distinct()
->whereNull('num_pre')
->where('status_ref', '>', 4)
->orderBy('claims_id')
->offset(0)
->limit($limite)
->pluck('claims_id'); // will get only the values
foreach ($claims as $index => $claimId) {
// use same where clauses
Refunds::query()
->whereNull('num_pre')
->where('status_ref', '>', 4)
->where('claims_id', $claimId) // add this one
->update(['num_pre' => 200 + $index]);
// num_pre needs to be in $fillable property
// or the model should have no guarded fields
}
Instead of using foreach you can use ->each():
$index = 0;
Application::query()
->select(['claims_id'])
->distinct()
->whereNull('num_pre')
->where('status_ref', '>', 4)
->orderBy('claims_id')
->offset(0)
->limit($limite)
// needs to close $index by reference
// as the closure mutates its value
->each(function ($claimId) use (&$index) {
Refunds::query()
->whereNull('num_pre')
->where('status_ref', '>', 4)
->where('claims_id', $claimId)
->update(['num_pre' => 200 + ($index++)]);
});
Alternative: If you need to select them prior you can use joinSub as MySQL won't let you use limit and offset in a "where in" subquery:
$limite = 100;
$refunds = Application::query()
->joinSub(function ($query) use ($limite) {
$query->select(['claim_id'])
->distinct()
->from('refunds')
->whereNull('num_pre')
->where('status_ref', '>', 4)
->orderBy('claims_id')
->offset(0)
->limit($limite);
}, 'claims', 'claims.claim_id', '=', 'refunds.claim_id')
->whereNull('num_pre')
->where('status_ref', '>', 4)
->orderBy('refunds.claims_id')
->get();
$claimIds = $refunds->pluck('claims_id')->unique()->values();
foreach ($claimIds as $index => $claimId) {
Refunds::query()
->whereNull('num_pre')
->where('status_ref', '>', 4)
->where('claims_id', $claimId)
->update(['num_pre' => 200 + $index]);
}
Hope it helps