7

I have a table named "items" and an input for the where condition named "ref_code".

$items = DB::table('items')
             ->where('ref_code','=', $request->ref_code)
             ->get(['id', 'ref_code', 'name','price']);

But I can't seem to take the values of each column.

I checked wether the query builder worked or not by using:

return $items;

Fortunately, there's no problem.

But returning or getting a single value doesn't work with:

return $items->id

Is my syntax wrong? All of this are inside my controller.

EDIT: I tried

dd($items);

before returning and it showed me this:

  Collection {#325 ▼
  #items: array:1 [▼
    0 => {#322 ▶}
  ]
}
3
  • P.S: I used POST method to throw the ref_code input from the blade. Commented Jul 22, 2017 at 4:45
  • $items would be a collection, hence you would need to loop through $items to get its properties Commented Jul 22, 2017 at 4:45
  • make dd($items) and update question with the result Commented Jul 22, 2017 at 4:46

3 Answers 3

8

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)
             ->first();

Or

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

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

Hope it will help.

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This worked like magic like the other answer. Big help guys. :)
Select my provided solution as an answer. So it will be helpful for me as well.
2

get() would return a collection

$items = DB::table('items')
         ->where('ref_code','=', $request->ref_code)
         ->get(['id', 'ref_code', 'name','price']);

In the above case $items would be a collection, hence you need to loop through the collection to access the properties

foreach ($items as $item) {
    $item->price;
}

If you'd need to return a Model instance you could use the method first() instead

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

and access the properties as

$items->price;

Comments

0

Try this using model

$result =  Model_name::where('ref_code','=', $request->ref_code)
                       ->first(['id', 'ref_code', 'name', 'price']);

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.