Laravel How to Return Single Column Value Using Query Builder

Laravel. Get single column values of a table using Query Builder

Thanks for updating your question with the result. Look at your debug result. it looks like

array:1 [▼
0 => {#322 ▶}

That means your query returning a collection of arrays because of you're using get() method. so get() method always return a collection of an array.

For avoiding this problem you have to use first() method instead of get().
Remember: all the time you have to use first() method when you want to get a single row.

So your query should be like :

$items = DB::table('items')
->select('id', 'ref_code', 'name', 'price')
->where('ref_code','=', $request->ref_code)


$item = YourModelName::select('id', 'ref_code', 'name', 'price')
->where('ref_code','=', $request->ref_code)

And finally get output as like
$item->id, $item->ref_code etc.

Hope it will help.


Laravel: getting a single value from a MySQL query


Sorry i forgot about pluck() as many have commented :
Easiest way is :

return DB::table('users')->where('username', $username)->pluck('groupName');

Which will directly return the only the first result for the requested row as a string.

Using the fluent query builder you will obtain an array anyway.
I mean The Query Builder has no idea how many rows will come back from that query.
Here is what you can do to do it a bit cleaner

$result = DB::table('users')->select('groupName')->where('username', $username)->first();

The first() tells the queryBuilder to return only one row so no array, so you can do :

return $result->groupName;

Hope it helps

How to get a single value from Query Builder in Laravel?

This is why you have ->value('my_column') method. So you'll end up with:

->where(['user_id' => $user_id, 'news_comment_id' => $newsComment->id])

The advantage is that the value is retrieved directly from the database. If you call first() before it, it can be null thus break your code.

is there a way to retrieve a single row's column in a database table in laravel's database query builder

Try change the sum code like this :

$sum = $debit->amt_debit + $amt->balance;

Because when you select one column it will return like this :

attributes: array:1 [
"amt_debit" => "your amount"

So you need to call that one column from your class.

Laravel Query Builder select return columns inside an array

SQL cannot get you the structure you want as far as I know. But you can use map() to restructure your data from your first query.

$data = $task_query->map(function($task) {
return [
'a' => [$task->column_1, $task->column_2, $task->column_3],
'material' => [$task->material_column_1, $task->material_column_2, $task->material_column_3],
'product' => [$task->product_column_1, $task->product_column_2, $task->product_column_3]

Then you can use your data in your structure

foreach($data as $item) {

Laravel query builder inject column value into Carbon query

I believe you can use whereRaw() in your clause to use raw DB expressions, If its MySQL you can use DATE_ADD method with your column value for comparision

$sql->where('domain_expires_on', '>', Carbon::now()->subDays(2)) // 2 days after expiry
->whereRaw('domain_expires_on < DATE_ADD(NOW(), INTERVAL domain_alert_period DAY)');

Related Topics

Leave a reply