Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get two columns value in laravel where first is key and 2nd is value?

Tags:

php

mysql

laravel

I have 2 columns id = [10 , 22 , 31]; and loction_url = [123.456 , 654.325 , 632,983]; i want data into a single array like this $a = [10 => 123.456 , 22 => 654.325 , 31 => 632,983];

here is my query which only gets columns

$customers = Customer::whereIn('created_by', $adminot)->select(array('id' , 'location_url'))->get();
like image 928
Hamza Qureshi Avatar asked Sep 05 '25 15:09

Hamza Qureshi


1 Answers

This is what Collection::pluck is for:

$customers = Customer::whereIn('created_by', $adminot)
    ->select(array('id' , 'location_url'))
    ->get()
    ->pluck('location_url', 'id');

It'll return an associative array where the key is the value of id and it's respective value is the value of location_url.

like image 50
Dan Avatar answered Sep 08 '25 11:09

Dan