SpencerChantler123
SpencerChantler123

Reputation: 366

Convert SQL retrieve result into a string in Laravel

I have a retrieve statement that goes like this

$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);
    }

which returns the result

enter image description here

What can I do to modify this $email to only get "[email protected]" as my result?

Upvotes: 1

Views: 1049

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You can use the value() method:

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

value('email') is a shortcut for ->first()->email

You may extract a single value from a record using the value method. This method will return the value of the column directly

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

Upvotes: 2

ndrwnaguib
ndrwnaguib

Reputation: 6115

You can use ->first() . In your case,

$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);
}

Upvotes: 1

Related Questions