0

I have this variable which I want to insert to the database. Basically I want to insert array of objects:

$data['matching_competitors'] = [
    [
        'id_project'            =>  $id_project, 
        'id_product'            =>  $product_id, 
        'link'                  =>  $link, 
        'id_product_competitor' =>  $id_competitor, 
        'link_competitor'       =>  ''
    ], 
    [
        'id_project'            =>  $id_project, 
        'id_product'            =>  $product_id, 
        'link'                  =>  $link, 
        'id_product_competitor' =>  $id_competitor, 
        'link_competitor'       =>  ''
    ], 
    
];

$insert_matching = DB::connection( $db_name )
                ->table( $db_name . '.product_matching')
                ->insert( $data );

but I got an error:

"Array to string conversion"

5
  • Your best bet is encoding it to JSON, saving it, and upon retrieval decoding it again. Or create a relation for this? Commented Feb 14, 2023 at 14:36
  • Do you mean this $data = json_encode($data); Commented Feb 14, 2023 at 14:38
  • 4
    Is each array a new row? If so, you have one too many array levels. Assign that array to $data instead of $data['matching_competitors'] Commented Feb 14, 2023 at 14:40
  • Is that a typo ? ->insert( $data['matching_competitors'] ); Commented Feb 14, 2023 at 14:45
  • @aynber You are correct, thats' what I need :) Commented Feb 14, 2023 at 14:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.