【问题标题】:PHP: Return id of the updated row in Laravel 4PHP:返回 Laravel 4 中更新行的 id
【发布时间】:2014-11-28 19:18:03
【问题描述】:

我试图在我的 Laravel 应用程序中返回最后更新的 ID。我在数据库中插入了一笔付款。当 PayPal 退款时,IPN 会将 payment_status 更新为“已退款”。现在更新后需要返回更新列的 id。但我越来越空了。这是我尝试过的。

第一种方法:

    $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;
}

第二种方法:

$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;
}

如何检索更新后的 id?

【问题讨论】:

  • whereTxnId 是你的方法吗?你能告诉我们你的方法是什么吗?
  • 更新后的id是什么意思?
  • 我的意思是我需要主键,即更新行的“id”。
  • whereTxnId($parent_txn_id) 是 Laravel 中的动态方法。它解析为where('txn_id', '=', 'parent_txn_id')

标签: php mysql laravel laravel-4


【解决方案1】:

用你的第二种方法使用

$is_updated->id

【讨论】:

  • 第二种方法甚至不更新数据库,而第一种方法有效,但我没有得到更新行的 id。你知道为什么吗?
【解决方案2】:

我使用下面的代码修复了它。我不知道这段代码是否有效,但它解决了我的问题。

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;

    }

【讨论】:

    猜你喜欢
    • 2013-05-11
    • 2013-03-28
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多