Laravel控制器显示错误尝试获取非对象的属性``地址''

时间:2019-04-29 15:12:04

标签: php laravel controller

  

将带有以太坊地址的数组传递给控制器​​会告诉我   试图获取非对象的属性“地址”的错误

我正在使用Laravel和区块链以太坊开发Dapp,我已经配置了自己的以太坊节点

我有一个带有公共函数的BaseController,该公共函数将JSONRPC调用到URL API并向RegisterController返回一个地址,但是它出现了试图在代码行“地址”中获取非对象的属性“地址”的错误。 => $ taddress->地址

有人可以帮我吗?

我留下每个控制器的代码

BaseController

public function get_address()
{
    $fields = [
        "jsonrpc" => '2.0',
        "method"  => 'eth_accounts',
        //  "method" => 'personal_newAccount',
        //  "method" => 'createAddressForToken',
        "params"  => ['password'],
        "id"      => '1',
    ];
    $fields = json_encode($fields);
    $headers = [
        'Content-Type: application/json',
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, APIURL);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    $result = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($result);
    if (isset($response->error->code)) {
        return $address = 0;
    } else {
        return $address = $response->result;
    }
}

RegisterController

protected function create(array $data)
{
    $settings = AdminSettings::first();

    // Verify Settings Admin
    if ($settings->email_verification == 1) {

        $confirmation_code = str_random(100);
        $status = 'pending';

        //send verification mail to user         
        $_username = $data['name'];
        $_email_user = $data['email'];
        $_title_site = $settings->title;
        $_email_noreply = $settings->email_no_reply;

        Mail::send('emails.verify', ['confirmation_code' => $confirmation_code, 'title_site' => $_title_site],
            function ($message) use (
                $_username,
                $_email_user,
                $_title_site,
                $_email_noreply
            ) {
                $message->from($_email_noreply, $_title_site);
                $message->subject(trans('users.title_email_verify'));
                $message->to($_email_user, $_username);
            });

    } else {
        $confirmation_code = '';
        $status = 'active';
    }

    $token = str_random(75);

    $taddress = $this->get_address();
    json_encode($taddress);
    var_dump($taddress);

    return User::create([
        'name'              => $data['name'],
        'email'             => $data['email'],
        'password'          => bcrypt($data['password']),
        'countries_id'      => $data['countries_id'],
        'avatar'            => 'default.jpg',
        'status'            => $status,
        'role'              => 'normal',
        'token'             => $token,
        'confirmation_code' => $confirmation_code,
        'address'           => $taddress->address,
        //'address' => $taddress,
        'private_key'       => $taddress->password,
    ]);
}

感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

您的return语句中有错误。您将返回变量赋值的结果,而应该只返回变量。

这导致行$taddress = $this->get_address();不是对象,而是很可能是布尔值。这会导致您的错误。

if (isset($response->error->code)) {
    return $address = 0;
} else {
    return $address = $response->result;
}

应更改为此:

if (isset($response->error->code)) {
    return 0;
} else {
    return $response->result;
}

答案 1 :(得分:0)

非常感谢您的回答!

我做了更改,就像他们在BaseController中这样告诉我:

if(isset($response->error->code))
    {
        return $address=0;  
    }
    else
    {   
        return $response->result;
        //return $address=$response->result;
    }

但是它在代码行的RegisterController中始终显示相同的错误:

'address' => $taddress->address

那会是什么