PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 17, 2022

[FIXED] How to make a job to save an object as json in the database in Laravel?

 May 17, 2022     job-scheduling, json, laravel, laravel-queue, php     No comments   

Issue

I'm trying to make a job that saves the HTTP response as json in the database using the handle method, this is my code:

   public function handle()
{
    $syncedResults = $this->guzzleGet();
    Rfm::truncate();
    
    /**
     * @var \Illuminate\Database\Eloquent\Model $rfm
     */
    Rfm::create(['RFM' => $syncedResults]);

 
}

public function guzzleGet()
{
    $aData = [];
    $sCursor = null;

    while($aResponse = $this->guzzleGetData($sCursor))
    {
        if(empty($aResponse['data']))
        {
            break;
        }
        else
        {

            $aData = array_merge($aData, $aResponse['data']);


            if(empty($aResponse['meta']['next_cursor']))
            {
                break;
            }
            else
            {
                $sCursor = $aResponse['meta']['next_cursor'];
            }
        }
    }
    

    
    return json_encode($aData);

here is my model :

<?PHP

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Rfm extends Model
{
use HasFactory;
protected $fillable = ['RFM'];  
}

And the migrations:

   public function up()
{
    Schema::create('rfms', function (Blueprint $table) {
        $table->id();
        $table->json('RFM');
        $table->timestamps();
    });
}

now I made a signature command to run the worker but every time it fail with this error in the log:

TypeError: Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, string given, called in C:\xampp\htdocs\clv\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 920 and defined in C:\xampp\htdocs\clv\vendor\laravel\framework\src\Illuminate\Database\Grammar.php:136

i wonder if I'm doing something wrong?!


Solution

i have found a workaround in the meanwhile:

just inserting the data using the Query Builder instead of eloquent, Like this:

public function handle()
{
    $syncedResults = $this->guzzleGet();
    Rfm::truncate();
    
    /**
     * @var \Illuminate\Database\Eloquent\Model $rfm
     */
    DB::table('rfms')->insert(['RFM' => json_encode($syncedResults)]);

}


Answered By - Amjad Alarori
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing