Laravel Eloquent:getDictionary,对象值作为结果值
目前,Laravel Eloquent:getDictionary,对象值作为结果值,laravel,eloquent,laravel-5.2,Laravel,Eloquent,Laravel 5.2,目前,$mymodel->getDictionary()返回: 我要找的是: "7gct5YaTvuxBmY2" => "Leadership", "7NrXZepqczMSHqM" => "...", "..." => "...", ... 我做到这一点的唯一方法是: $construct_obj = OrganizationalConstruct::where('is_root', 0)->where('organization_id', $this->cu
$mymodel->getDictionary()代码>返回:
我要找的是:
"7gct5YaTvuxBmY2" => "Leadership",
"7NrXZepqczMSHqM" => "...",
"..." => "...",
...
我做到这一点的唯一方法是:
$construct_obj = OrganizationalConstruct::where('is_root', 0)->where('organization_id', $this->current_company->company_id)->get();
$constructs = [];
$constructs[''] = '';
for ($i = 0; $i < count($construct_obj); $i++) {
$constructs[$construct_obj[$i]->organizational_construct_id] = $construct_obj[$i]->construct_name;
}
$construct\u obj=OrganizationalConstruct::where('is\u root',0)->where('organization\u id',$this->current\u company->company\u id)->get();
$constructs=[];
$constructs['']='';
对于($i=0;$iorganization\u construct\u id]=$construct\u obj[$i]->construct\u name;
}
是否有更简单的方法获取格式“key”=>“specific column value”
我试过:
- 凯比
- 列表
- 获取词典
- 地图
事实上,这是一个非常简单的答案。看起来
列表
方法可以接受多个参数,允许我将id
作为参数1传递,将名称
作为参数2传递,在一行中给出键=>值
的所需结果
因此:
$construct_obj = OrganizationalConstruct::where('is_root', 0)->where('organization_id', $this->current_company->company_id)->get();
$constructs = [];
$constructs[''] = '';
for ($i = 0; $i < count($construct_obj); $i++) {
$constructs[$construct_obj[$i]->organizational_construct_id] = $construct_obj[$i]->construct_name;
}
希望这对其他人有帮助。其实答案很简单。看起来列表
方法可以接受多个参数,允许我将id
作为参数1传递,将名称
作为参数2传递,在一行中给出键=>值
的所需结果
因此:
$construct_obj = OrganizationalConstruct::where('is_root', 0)->where('organization_id', $this->current_company->company_id)->get();
$constructs = [];
$constructs[''] = '';
for ($i = 0; $i < count($construct_obj); $i++) {
$constructs[$construct_obj[$i]->organizational_construct_id] = $construct_obj[$i]->construct_name;
}
希望这对其他人有所帮助。您应该直接在查询中调用pull
,这样您就不会下拉所有模型的所有属性:
$dictionary=OrganizationalConstruct::where('is_root',0)
->其中('organization\u id',$this->current\u company->company\u id)
->pull('construct_name','organization_construct_id');
注意:列表已弃用,将在Laravel 5.3中删除。改用pull
方法。您应该直接在查询中调用pull
,这样您就不会下拉所有模型的所有属性:
$dictionary=OrganizationalConstruct::where('is_root',0)
->其中('organization\u id',$this->current\u company->company\u id)
->pull('construct_name','organization_construct_id');
注意:列表已弃用,将在Laravel 5.3中删除。请改用pluck
方法。显然列表
方法将在Laravel 5.3中删除,因此请改用pluck
方法。因此,我们已将Joseph的答案标记为正确。显然,Laravel 5.3中将删除列表
方法,因此请改用拔出
方法。因此,我已将约瑟夫的回答标记为正确。