Laravel / Lumen:如何从匹配WHERE子句的第一个记录返回单个值

I'm attempting to return a single column value from the first record matching my where WHERE clause. In the below example from my controller, I'm defining this result as $code. I can either get the first record or I can get a specific column value but not both.

First Record: $code = DB::table('codes')->where('redeemed', '0')->first();

Single Column Value: $code = DB::table('codes')->where('redeemed', '0')->value('code');

How can I do both in a single request?

What you are looking for is pluck.

DB::table('codes')->where('redeemed', 0)->pluck('code');

You can use the select() method:

DB::table('codes')->where('redeemed', '0')->select('code')->first();

Your second example:

$code = DB::table('codes')->where('redeemed', '0')->value('code');

This will already do what you want. The value() method calls first(), as can be seen in the definition of the method:

public function value($column)
{
    $result = (array) $this->first([$column]); // <-- call to first on query

    return count($result) > 0 ? reset($result) : null;
}