1

I am trying to execute a query using eloquent, however it returns me an array. What I want is to only get the string value of my query.

This is what I get

[{"name":"hey"}] [{"name":"sdasdasd"}]

Here's my eloquent query:

 $categoryName = Category::select('name')->where('id', request('category'))->get();
 $subCategory = Subcategory::select('name')->where('id', request('subCat'))->get();
5
  • 1
    if you always get single value then use first in place of get . it will return single object Commented Sep 4, 2019 at 10:14
  • try with pluck eloquent Commented Sep 4, 2019 at 10:14
  • 1
    $titles = DB::table('roles')->pluck('title'); Commented Sep 4, 2019 at 10:15
  • 1
    [{"name":"hey"}] [{"name":"sdasdasd"}] aren't arrays, they are instances of Collections which means that when you iterate over them, you can access their values using the ->name syntax, rather than array syntax: ['name'] :) fyi Commented Sep 4, 2019 at 10:22
  • @party-ring Oh sorry I'm not good with terminologies. :) Thank you for the tip! Commented Sep 4, 2019 at 10:27

5 Answers 5

3

as per the Laravel Docs

Retrieving A Single Row / Column From A Table If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single stdClass object:

$user = DB::table('users')->where('name', 'John')->first();

echo $user->name;

If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:

$email = DB::table('users')->where('name', 'John')->value('email');

so in your example maybe you need to do something like the following :

$subCategory = Subcategory::select('name')->where('id', request('subCat'))-> first();

echo $subCategory->name;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was thinking of the native PHP kind of query. Thank you!
0

There are so many ways to achive your goal.

Solutions

  1. Use pluck().
$categoryName = Category::select('name')
                ->where('id', request('category'))
                ->pluck()
                ->first();
  1. Use model properties.
$categoryName = Category::find(request('category'))
                ->name;

Comments

0

If you take only name then follow the below code.

$categoryName = Category::where('id', request('category'))->first()->name;
$subCategory = Subcategory::where('id', request('subCat'))->first()->name;

echo $categoryName;
echo $subCategory;

If you take all columns in a row follow the below code:

$categoryName = Category::where('id', request('category'))->first();
$subCategory = Subcategory::where('id', request('subCat'))->first();

echo $subCategory->name;
echo $categoryName->name; 

I hope this i may help you.Try this.

Comments

0

You can get the answer using

$name = DB::table('Category')->where('id','=',request('category'))->pluck('name')->first();

echo $name;

Comments

0

$name = Category::select('name')->where('id', request('category'))->take(1)->orderBy('id','asc')->first()->name;

echo $name;

OR

echo Category::select('name')->where('id', request('category'))->take(1)->orderBy('id','asc')->first()->name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.