【问题标题】:Laravel/Vue.js - Trying to get property of non-object. Can access it in Vue but not LaravelLaravel/Vue.js - 试图获取非对象的属性。可以在 Vue 中访问,但不能在 Laravel 中访问
【发布时间】:2018-09-10 09:00:35
【问题描述】:

我遇到了一个奇怪的问题,我似乎无法访问 Laravel 中的对象,但是当将变量传递到我的 Vue 前端时,我可以访问其中的所有内容。

这是代码,这是它产生的...

public function getPaymentStatus(Request $request) {

    $transaction = $this->midTest->getLatestTransaction();

    $url = "https://" . $transaction->url . $request->resourcePath;
    $url .= "?authentication.userId=" . $transaction->user_id;
    $url .= "&authentication.password=" . $transaction->password;
    $url .= "&authentication.entityId=" . $transaction->entity_id;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // This should be set to true in production
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $responseData = curl_exec($ch);
    if(curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);

    return $responseData;
}

如果我这样做,我会得到一个错误。

return $responseData->data;

试图获取非对象的属性“数据”{“异常”:“[对象] (ErrorException(代码:0):试图获取非对象的属性“数据” 在 /Users/dallysingh/Projects/Laravel/app/Http/Controllers/MidTestController.php:196)

在我的 Vue 前端中,我可以控制台记录以下内容并清楚地查看所有数据。

console.log(response.data);

如何访问 Laravel 中的对象,因为我正在尝试将数据存储在表中,然后再将其发送到我的 Vue 前端。这就是我想要的……

public function getPaymentStatus(Request $request) {

    $transaction = $this->midTest->getLatestTransaction();

    $url = "https://" . $transaction->url . $request->resourcePath;
    $url .= "?authentication.userId=" . $transaction->user_id;
    $url .= "&authentication.password=" . $transaction->password;
    $url .= "&authentication.entityId=" . $transaction->entity_id;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // This should be set to true in production
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $responseData = curl_exec($ch);
    if(curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);

    $this->midTest->updateLatestTransaction($responseData, $transaction);

    return $responseData;
}

public function updateLatestTransaction($response, $transaction) {
    $transaction = DB::table('transactions')
                        ->where('id', $transaction->id)
                        ->update(
                            ['transaction_id', $response->data['id']
                        ]);
}

【问题讨论】:

  • 你能转储 $responseData 吗? laravel 在里面找不到数据属性
  • 使用 Guzzle 代替 curl_setopts 的怪物。祝你好运!
  • @HasanTıngır - 当我将 $responseData 传递给 Vue 前端时,我可以访问 data 属性,但我似乎无法通过 Laravel 访问它。我正在尝试将 responseData 传递给模型以将数据插入表中,但它无法正常工作,因为它找不到它。
  • 代码已更新为包含模型。

标签: php laravel curl


【解决方案1】:

$responseData 在 PHP 中可能是一个 json 字符串。

试试

$responseDataDecoded = json_decode($responseData);

现在变量 $responseDataDecoded 是一个对象。

在 laravel 中你也可以使用函数 dd($variable) 来转储变量。 例如解码后的 json 字符串:

dd($responseDataDecoded);

【讨论】:

  • 如果我想从对象中访问某些东西,我是否只需执行以下操作... $responseDataDecoded->data['id']
  • 试过你说的,我现在在尝试使用 Model 函数将值插入表时遇到以下错误...未定义的属性:stdClass::$data {"exception": "[object] (ErrorException(code: 0): Undefined property: stdClass::$data at /Users/dallysingh/Projects/Laravel/app/Http/Models/MidTest.php:46)
  • de $responseDataDecoded 上是否存在数据属性。您可以使用 dd($variable) 函数检查变量包含的属性。
猜你喜欢
  • 2022-10-08
  • 1970-01-01
  • 2020-10-23
  • 2018-06-27
  • 1970-01-01
  • 2015-08-19
  • 2019-03-27
  • 2018-03-07
相关资源
最近更新 更多