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

AquinoBR's avatar

Code Update for PHP 7

Some time ago in a system I created, I did a unified deposit check, it just checked if an address made the deposit and then added the balance amount and the history.

Now I'm creating another system, however, using Laravel 5.6 and I'm rewriting some codes, but I do not know how to do all of the caches instead of just 1. I need to know how to do a coding with FOR or FOREACH to check all at once.

Old page.

<?php
require_once 'vlogin.php';
require_once 'config/configDB.php';
require_once 'api/block_io.php';
require_once 'certific.php';
$apiKey = '';
$pin = '';
$version = 2;
$block_io = new BlockIo($apiKey, $pin, $version);

try {
  $addressBtcLabel = $block_io->get_address_by_label(array('label' => $nick));
    $addressDepositBtc = $addressBtcLabel->data->address;

    $checkdeposit = $block_io->get_transactions(array('type' => 'received', 'addresses' => $addressDepositBtc));

    if ($checkdeposit->status = "success" && $checkdeposit->data->txs[0]->confirmations >= 2) {
    //criação de variaveis menores
    $statusCheck = $checkdeposit->status;
    $txidCheck = $checkdeposit->data->txs[0]->txid;
    $confirmationsCheck = $checkdeposit->data->txs[0]->confirmations;
    $recipientCheck = $checkdeposit->data->txs[0]->amounts_received[0]->recipient;
    $amountCheck = $checkdeposit->data->txs[0]->amounts_received[0]->amount;

    // verifica se o txid já existe no banco de dados
    $queryTxid = "SELECT COUNT(txid) as SOMA FROM depositBtc WHERE txid='$txidCheck'";
    $cTxid = mysqli_query($conn, $queryTxid) or die(mysqli_error());
    $consultTxid = mysqli_fetch_assoc($cTxid);
    $txidverific = $consultTxid['SOMA'];

    //atualiza a balança do jogador
    if ($txidverific == 0) {
    $upbalance = $balance + $amountCheck;
    $queryCredit = "UPDATE players SET balance = '$upbalance' WHERE nick = '$nick'";
    $conectCredit = mysqli_query($conn, $queryCredit);

    //cria um deposito no banco de dados
    $today = date('Y-m-d H:i:s');
    $queryDeposit = "INSERT INTO depositBtc (idDeposit, datetoday, txid, amount, status, nick, confirmations, recipient) VALUES ('','$today','$txidCheck','$amountCheck','confirmed','$nick','$confirmationsCheck','$recipientCheck')";
    $conectDeposit = mysqli_query($conn, $queryDeposit);

    //create notificationsModal
    $queryNotifyDeposit = "INSERT INTO notify (id, nick, notify, datetoday) VALUES ('','$nick','Hello ".$nick."! Added ".$amountCheck." LCOIN in your balance.','$today')";
    $conectNotifyDeposit = mysqli_query($conn, $queryNotifyDeposit);

    } else {
    echo "";
    }

    } else {
    echo "";
    }

  } catch (Exception $e) {
    echo "";
  }

The old page looks for a specific array and performs the functions of updating the balance, history and sending notification.

I want to know how I can do to create a loop in the entire array and not in item [0].

{#272 ▼
  +"status": "success"
  +"data": {#274 ▼
    +"network": "DOGETEST"
    +"txs": array:26 [▼
      0 => {#289 ▼
        +"txid": "43183135ae536e339fc82eafed9cde68b6fb89e5475675cc9a62c6539ad41908"
        +"from_green_address": true
        +"time": 1522183284
        +"confirmations": 4
        +"amounts_received": array:1 [▼
          0 => {#284 ▼
            +"recipient": "2N6chEr7VY3qqysPCgftAQzBHj4msXWjEbc"
            +"amount": "4.10000000"
          }
        ]
        +"senders": array:1 [▶]
        +"confidence": 1.0
        +"propagated_by_nodes": null
      }
      1 => {#288 ▶}
      2 => {#283 ▶}
      3 => {#287 ▶}
      4 => {#291 ▶}
      5 => {#293 ▶}
      6 => {#295 ▶}
      7 => {#297 ▶}
      8 => {#299 ▶}
      9 => {#301 ▶}
      10 => {#303 ▶}
      11 => {#305 ▶}
      12 => {#307 ▶}
      13 => {#309 ▶}
      14 => {#311 ▶}
      15 => {#313 ▶}
      16 => {#315 ▶}
      17 => {#317 ▶}
      18 => {#319 ▶}
      19 => {#321 ▶}
      20 => {#323 ▶}
      21 => {#325 ▶}
      22 => {#327 ▶}
      23 => {#329 ▶}
      24 => {#331 ▶}
      25 => {#333 ▶}
    ]
  }
}

I started creating a function in the controller to replace the old page with a function. I started creating and I stopped here.

public function depositBTCCheck()
    {
        $apiKey = config('larablockio.apiKeyBTC');
        $obj = new LaraBlockIo($apiKey);
        $checkdeposit = $obj->getReceivedTransactions();
        dd($checkdeposit);

        //HERE ... I WANT TO KNOW HOW TO DO THE LOOP NO ARRAY $checkdeposit
        foreach ($checkdeposit as &$value) {

            if ($checkdeposit->status = 'success' && $checkdeposit->data->txs[0]->confirmations >= 2){

            }   
        }
    }
0 likes
4 replies
biishmar's avatar

@AquinoBR You better learn laravel first before you start coding on laravel framework, they have lots of resources to help through your problem

AquinoBR's avatar

@smagic39 I'll try with your tip.

@biishmar The problem that does not have time to study the entire framework, the project is already in development and I have deadline to finish it. Then I'll take it easy.

AquinoBR's avatar

@smagic39

My dear, I am progressing with your tip, Now I stopped in an error that must be something easy, but I can not solve it is this:

In the database has a table called Bitcoin, where it has a model, it has the following columns [id, user_id, name, amount] I am using the commented line in the code below to check if there is any data with the user_id pulling in the array , if it does not exist then create a new record with "firstOrCreate". The problem is not passing the creation of this record to another user without being logged in. How can I do to create the data without being logged in by the user, but what is pulled by the array?

public function depositBTCCheck(Historic $historics)
    {
        $user = auth()->user();
        $addressbtc = $user->addressbtc;
        $apiKey = config('larablockio.apiKeyBTC');
        $obj = new LaraBlockIo($apiKey);
        $checkdeposit = $obj->getReceivedTransactions();
        $checkdepositItem = collect($checkdeposit->data->txs);
        $checkdepositTotal = $checkdepositItem->count();
        $checkdepositArray = $checkdepositItem->toArray();
            
        for($i=0; $i<$checkdepositTotal;$i++) {
            //datos array
            $txidCheck = $checkdepositArray[$i]->txid;
            $confirmationsCheck = $checkdepositArray[$i]->confirmations;
            $recipientCheck = $checkdepositArray[$i]->amounts_received[0]->recipient;
            $amountCheck = $checkdepositArray[$i]->amounts_received[0]->amount;
            //check txid DB
            $historic = $historics->select('status')->where('status', $txidCheck);
            
            if ($historic == false) {
                
            } else {
                $users = \App\User::get();
                $user = $users->where('addressbtc', $recipientCheck);
                $userId = 2;//user id by deposit
                $bitcoins = \App\Models\Bitcoin::get();
                $bitcoin = $bitcoins->where('user_id', $userId)->first();
                $balanceBTC = $bitcoin->balanceBTC()->firstOrCreate([]);//<-error
        //"Call to a member function balanceBTC() on null"
                dd($balanceBTC);
            }
        }
    }

User.php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\PasswordSecurity;
use App\Models\Bitcoin;


class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'lname', 'email', 'password', 'image', 'numbercel',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token', 'pin',
    ];

    public function passwordSecurity()
    {
        return $this->hasOne(PasswordSecurity::class);
    }

    public function balanceBTC()
    {
        return $this->hasOne(Bitcoin::class);
    }


Please or to participate in this conversation.