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

seyidtakele's avatar

get Value Only from laravel Collection result

I have the following query(the actual query is too long and contains DB::raw so the query here is for question only)

$query=DB::table(DB::raw('users@KVDBLINK'))->select('name','id','email')->limit(2)->get();

the result is

[ {"name":"Dayle","id":1,"email":"dayle213@gmail.com"}, {"name":"John","id":2,"email":"[email protected]"} ]

but i want the result like

[ {"Dayle",1,"dayle213@gmail.com"}, {"John",2,"[email protected]"} ]

NB: this might not be the right place to ask but in stackoverflow it seems i didnot get much help. I need array of values so i can insert in bulk .

0 likes
6 replies
seyidtakele's avatar

that does not help at all. the result is the same as the $query

CorvS's avatar
CorvS
Best Answer
Level 27

@seyidtakele@gmail.com You could use transform():

$result->transform(fn($user) => [$user->name, $user->id, $user->email])
1 like
Snapey's avatar

you want just the array values and not the keys?

why would having keys make it difficult to insert in bulk?

seyidtakele's avatar

@snapey .

yes , i need arrray of values only

DB::table(DB::raw('users@KVDBLINK'))->select('name','id','email')->orderBy('id')
->chunk(500, function ($users) {
    DB::table('staff')->insertOrIgnore($users);   //// type error expects Array  for this i am trying to convert the result  collection in to array(array of values)


    
    foreach ($users as $user) {
     DB::table('staff')->insertOrIgnore((array)$user);  //does work but very very slow i guess it is inserting everysingle row at atime
    }

Another option to insert in bulk from Query result collection is appreciated

Please or to participate in this conversation.