Laravel Eloquent: getDictionary with object value as value of result -


currently, $mymodel->getdictionary(); returns:

this

what looking this:

"7gct5yatvuxbmy2" => "leadership", "7nrxzepqczmshqm" => "...", "..." => "...", ... 

the way have managed is:

$construct_obj = organizationalconstruct::where('is_root', 0)->where('organization_id', $this->current_company->company_id)->get(); $constructs = []; $constructs[''] = ''; ($i = 0; $i < count($construct_obj); $i++) {     $constructs[$construct_obj[$i]->organizational_construct_id] = $construct_obj[$i]->construct_name; } 

is there easier way of getting format "key" => "speific-column-value" ?

i have tried:

  • keyby
  • lists
  • getdictionary
  • map

you should call pluck directly on query, don't pull down attributes models:

$dictionary = organizationalconstruct::where('is_root', 0)                   ->where('organization_id', $this->current_company->company_id)                   ->pluck('construct_name', 'organizational_construct_id'); 

note: lists deprecated, , removed in laravel 5.3. use pluck method instead.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -