将SQL检索结果转换为Laravel中的字符串

问题描述 投票:1回答:2

我有一个像这样的检索语句

$arrayAID = request('agentID');
    $sizeAID = count($arrayAID);
    $aidList = "null";
    for($a = 0; $a < $sizeAID; $a++){
        $email = DB::table('insuranceAgents')
                 ->select('email')
                 ->where('agentID', '=', $arrayAID[$a])
                 ->get();
        dd($email);
    }

返回结果

enter image description here

我该怎么做才能修改这个$ email,只能获得“[email protected]”作为我的结果?

php laravel
2个回答
1
投票

您可以使用value()方法:

DB::table('insuranceAgents')->where('agentID', $arrayAID[$a])->value('email');

value('email')->first()->email的捷径

您可以使用value方法从记录中提取单个值。此方法将直接返回列的值

https://laravel.com/docs/5.5/queries#retrieving-results


1
投票

你可以使用->first()。在你的情况下,

$arrayAID = request('agentID');
$sizeAID = count($arrayAID);
$aidList = "null";
for($a = 0; $a < $sizeAID; $a++){
    $email = DB::table('insuranceAgents')
             ->select('email')
             ->where('agentID', '=', $arrayAID[$a])
             ->get()->first();
    dd($email);
}
© www.soinside.com 2019 - 2024. All rights reserved.