首页 > 解决方案 > Laravel:如何从嵌套数组中检索数据

问题描述

我正在尝试从数据库中的表中检索特定列。列名是verification_type_id,表名是certificate_verification_type

当我vardump()使用用于检索列的变量时,我得到的是:

array(2)
{
    [14]=> array(2)
    {
        ["certificate_id"]=> int(9)
        ["verification_type_id"]=> int(2)
    }
    [15]=> array(2)
    {
        ["certificate_id"]=> int(9)
        ["verification_type_id"]=> int(3)
    }
}

这是我的 laravel 雄辩的查询:

$certVeriType = CertificateVerificationType::all()->where('certificate_id', $certType)->toArray();

我怎样才能只从表中检索“verification_type_id”列。这是表的迁移文件:

$table->integer('certificate_id');
$table->integer('verification_type_id');
$table->index('certificate_id');

提前致谢!

标签: phplaravellaravel-5

解决方案


添加 -> pluck() (pluck 方法检索给定键的所有值)到您的雄辩查询:

$certVeriType = CertificateVerificationType::all()
    ->where('certificate_id', $certType)
    ->pluck('verification_type_id')
    ->toArray();

推荐阅读