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

salmankhan2482's avatar

Query is not working properly ?

protected function updateFulfillment(array $fulfillments): void
    {
        foreach ($fulfillments as $fulfillment) {
            $fulfillmentRecord = Fulfillment::where('line_item_code', $fulfillment['line_item_code'])
                ->where('fulfillment_code', $fulfillment['fulfillment_code'])->first();

            if ($fulfillmentRecord) {
                $fulfillmentRecord->update([
                    'refunded' => true,
                    'returned_quantity' => $fulfillment['quantity']
                ]);

            }
        }

        //what is the difference between the above and this one below query?
        // The above query update two records and this below query updates a single record
        foreach ($fulfillments as $fulfillment) {
            Fulfillment::where('line_item_code', $fulfillment['line_item_code'])
                ->where('fulfillment_code', $fulfillment['fulfillment_code'])
                ->update([
                    'refunded' => true,
                    'returned_quantity' => $fulfillment['quantity']
                ]);
        }

    }
0 likes
6 replies
jlrdw's avatar

I suggest using some dd in there to help troubleshoot.

Also maybe toSql().

Tray2's avatar

I agree with @jlrdw you need to take a look at the SQL generated from those two queries.

I also would suggest that you use a timestamp in your refunded column rather than a boolean. Boolean have notoriously badly support in many databases, and it would also tell you when something was refunded for free.

salmankhan2482's avatar

there are so many issues i found it while debugging

  1. in my model i have declared fulfillment_code as primary key

  2. there are 2 records with same fulfillment_code and i am shocked that when i have delcared this key as primary then how duplicates are able to be inserted

  3. and the most shocking was for me, that first snippet get a single records and i stored it in $fulfillmentRecord variable but when i update it it update 2 records instead of just single record which is stored $fulfillmentRecord.

salmankhan2482's avatar
update `fulfillments` set `returned_quantity` = ?, `refunded` = ?, `fulfillments`.`updated_at` = ? where `fulfillment_code` = ?
jlrdw's avatar

@salmankhan2482 Forgot to mention, check bindings, try:

dd($query->toSql(), $query->getBindings());

Adjust to your query.

salmankhan2482's avatar

but how it only checks for fulfillment_code when i already added checks for both line_item_code and fulfillment_code ?

Please or to participate in this conversation.