GregorSams
1 year ago

UpdateOrCreate does not insert anything within my db

Posted 1 year ago by GregorSams

I am using Laravel 5.5 and would like to insert data in my table. However, laravel does not insert any data in the db:

My table looks like the following:

CREATE TABLE `prices` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `exchanges_id` int(11) NOT NULL,
 `symbol` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
 `ask` decimal(40,9) NOT NULL,
 `created_at` timestamp NULL DEFAULT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

My model has the following fillables:

protected $fillable = ['exchanges_id', 'symbol', 'ask', 'exch_datetime']

My laravel command looks like the following. I am using the cctx lib to get prices:

 <?php

namespace App\Console\Commands;

use App\Prices;
use ccxt\ccxt;
use Illuminate\Console\Command;
use Log;

class TestCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Test:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $poloniex = new \ccxt\poloniex();
        $this->updatePrices($poloniex, "poloniex");
    }
    public function updatePrices($exchangeObj, $exchangeName)
    {
        $tick = $exchangeObj->fetch_ticker('GAS/BTC');
        var_dump($tick);
        try {
            print_r("#######################" . "\n");
            print_r($tick['datetime'] . "\n");
            print_r($tick['ask'] . "\n");

            $prices = Prices::updateOrCreate([
            //    'exchanges_id' => 10,
                'exch_datetime' => $tick['datetime'],
            ], [
                'ask' => $tick['ask'],
            ]);
            dd($prices->toSql());

        } catch (\Exception $e) {
            Log::info($e->getMessage());
        }
    }
}

I am getting the following output:

array(18) {
  'symbol' =>
  string(7) "GAS/BTC"
  'timestamp' =>
  string(13) "1513888309387"
  'datetime' =>
  string(29) "2017-12-21T20:31:49.647+00:00"
  'high' =>
  double(0.00219998)
  'low' =>
  double(0.00195)
  'bid' =>
  double(0.00196001)
  'ask' =>
  double(0.00198305)
  'vwap' =>
  NULL
  'open' =>
  NULL
  'close' =>
  NULL
  'first' =>
  NULL
  'last' =>
  double(0.00198328)
  'change' =>
  double(0.00064076)
  'percentage' =>
  NULL
  'average' =>
  NULL
  'baseVolume' =>
  double(48314.74516945)
  'quoteVolume' =>
  double(99.5599155)
  'info' =>
  array(10) {
    'id' =>
    int(198)
    'last' =>
    string(10) "0.00198328"
    'lowestAsk' =>
    string(10) "0.00198305"
    'highestBid' =>
    string(10) "0.00196001"
    'percentChange' =>
    string(10) "0.00064076"
    'baseVolume' =>
    string(11) "99.55991550"
    'quoteVolume' =>
    string(14) "48314.74516945"
    'isFrozen' =>
    string(1) "0"
    'high24hr' =>
    string(10) "0.00219998"
    'low24hr' =>
    string(10) "0.00195000"
  }
}
#######################
2017-12-21T20:31:49.647+00:00
0.00198305

My problem is that nothing gets inserted into the database.

I tried to use dd($prices->toSql()); with my query, but I do not get any sql output.

Any suggestions what might be wrong or how to debug this properly?

I appreciate your replies!

UPDATE I rechecked with your answers, however after trying I still get no record in the db. Because of some changes to my code I created a new question.

Please sign in or create an account to participate in this conversation.