Laravel 5 - database table for array with predefined index

Asked

Viewed 166 times

0

Is there any way to retrieve the results of a query in the database and transfer it to a Array with one of the table fields being index of Array?

You could achieve this result in two steps. After the query makes a loop and turns into the Array with desired index.

You can check this result in one step?

This is the code that returns me to query:

$lang = DB::table('language')->select('id', 'name')->get();

I wanted to $lang is an array of the type $lang[id=>name].

Show 1 more comment

1 answer

2


Try using the function Pluck

$lang = DB::table('language')->select('id', 'name')->pluck('name', 'id')->toArray();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.