0

I am trying to return the last updated ID in my Laravel application. I have a payment inserted into the database. When there is refund from PayPal the IPN will update the payment_status to "Refunded". Now after updating this needs to return the id of the updated column. But I am getting null. Here is what I have tried.

1st method:

    $is_updated = $this->whereTxnId($ipn_array['parent_txn_id'])
    ->update(array(
        'payment_status' => $ipn_array['payment_status'],
        'refund_txn_id' => $ipn_array['txn_id']
    ));
if($is_updated) {
    return $this->id;
}

2nd method:

$txn_id_matching = $this->whereTxnId($ipn_array['parent_txn_id']);

$txn_id_matching->payment_status = $ipn_array['payment_status'];
$txn_id_matching->refund_txn_id = $ipn_array['txn_id'];
$is_updated = $txn_id_matching->save();

if($is_updated) {
    return $txn_id_matching->id;
}

How can I retrieve the updated id?

SpiritOfDragon
  • 1,404
  • 2
  • 15
  • 27
  • Is `whereTxnId` your method? can you show us what inside of your method is? – Idham Perdameian Nov 28 '14 at 08:28
  • possible duplicate of [Laravel, get last insert id using Eloquent](http://stackoverflow.com/questions/21084833/laravel-get-last-insert-id-using-eloquent) – Joel Hinz Nov 28 '14 at 08:30
  • what do you mean by the updated id ? – lagbox Nov 28 '14 at 09:30
  • I meant I need the primary key i.e. the 'id' of the row that was updated. – SpiritOfDragon Nov 28 '14 at 09:37
  • The `whereTxnId($parent_txn_id)` is a dynamic method in Laravel. It parses to `where('txn_id', '=', 'parent_txn_id')` – SpiritOfDragon Nov 28 '14 at 09:49
  • Are you sure that the model update call is not returning false? Try doing a var_dump($is_updated); and check if is returning true. – Carlos Goce Nov 28 '14 at 10:31
  • Everything is working fine. I have checked. I get 1 for `$is_updated`. I have used the method 1 and it updates the DB and returns 1, but the 2nd method do not even update the DB. I think there is some error in that. – SpiritOfDragon Nov 28 '14 at 10:40
  • 1 What if there are more rows updated? 2 If it can't be, then why don't you simply fetch the row, then update, while it is sooo much easier? – Jarek Tkaczyk Nov 28 '14 at 12:08
  • 1. Beacsue PayPal returns a Unique Transaction ID each time, there cant be more than 1 row updated at a time. 2. I am doing the same thing. What I need is the ID of the updated row. When I update the row I need to return the ID and then put it into an array. – SpiritOfDragon Dec 01 '14 at 04:17

2 Answers2

0

With your second method use

$is_updated->id

Colin Schoen
  • 2,526
  • 1
  • 17
  • 26
  • the 2nd method doesn't even update the DB, while the 1st one works but I do not get the id of the updated row. Do you know why? – SpiritOfDragon Nov 28 '14 at 09:38
0

I fixed it using the below code. I do not know if this code is efficient but it fixed my issue.

public function unlockIpnUpdate($ipn_array) {

        $is_updated = $this->whereTxnId($ipn_array['parent_txn_id'])
            ->update(array(
                'payment_status' => $ipn_array['payment_status'],
                'refund_txn_id' => $ipn_array['txn_id']
            ));

        $txn_id_matching = $this->whereTxnId($ipn_array['parent_txn_id'])->first();

        if($is_updated) {
            return $txn_id_matching->id;
        }

        return false;

    }
SpiritOfDragon
  • 1,404
  • 2
  • 15
  • 27