Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Convert eloquent result to associative array in Laravel

How can convert the result from eloquent to associative array. I need to select tow column and have one as key and another as value. Here is the closet I got, however the value is an array. I want it to be only "my_value" column.

$array = Post::select('my_key','my_value')->get()->keyBy('my_key')
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You should use lists (Laravel 5.1) or pluck (Laravel 5.2+):

$array = Post::lists('my_value', 'my_key');

or

$array = Post::pluck('my_value', 'my_key');
about 4 years ago · Santiago Trujillo Report

0

I found a way to do so, I'm not sure whether it's proper way to do this performance wise though...

$array = Post::select('my_key','my_value')->get()->mapWithKeys(function ($item) {
            return [$item['my_key'] => $item['my_value']];
        })->toArray();
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!