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

kuruyiva's avatar

How to insert result from a query into another table ?

Hi Guys,

I have the following query

DB::table('product_sales') ->join('product_views', 'product_sales.session', '=','product_views.session') ->select('product_views.reference_id AS prod_view_id', 'product_sales.reference_id AS prod_bought_id', 'product_sales.session') ->take($num) ->get();

returning a json collection like the one bellow

[ { "prod_view_id": 202901, "prod_bought_id": 226122, "session":34343" }, { "prod_view_id": 202901, "prod_bought_id": 146606, "session": "4566" } ]

and I'm trying to save it into another table

foreach ($items as $item) { XTimesSessionBoughtProduct::insert($item); }

but I'm getting the following error message "must be of the type array, object given" which I understand, but I'm wondering how to return an array and not json object or is it another way I can do this ? Thank you for you time.

0 likes
2 replies
pardeepkumar's avatar
Level 2

Laravel ORM is best option for it

i think below code is help for you

$data = array(
    array('name'=>'Coder 1', 'rep'=>'4096'),
    array('name'=>'Coder 2', 'rep'=>'2048'),
    //...
);

Coder::insert($data);

for more information https://laravel.com/docs/5.0/eloquent

1 like

Please or to participate in this conversation.