4

I'm having a little problem with laravel that may be easily solved. To be short, the situation is: I have two tables, one for the users and the other one for products that has a column 'user_id' so I can identify the associated user.

In Laravel, I can use

$user = Sentry::getUser();        //Or Auth::user() if you're not using Sentry
$products = DB::table('table2')->where('user_id',$user->id);

And that should give me every product that user has. Good.

Now I want to show the products individually on screen, but unfortunately that doesn't work. It seems I can't echo this information in a string because it's made of multiple rows. I get

Object of class Illuminate\Database\Query\Builder could not be converted to string

For the solution, since the maximum associated products I allowed in the system is 3, I came up with the idea of getting each row separately and echoing them. For the first one, it's simple: $products->first(); but I have no idea on how to get the other two.

And maybe I'm being a newbie here, but I don't think I can use the products' id info since $products->id returns an error.

Can anyone help me?

Thanks in advance!

andrewsi
  • 10,807
  • 132
  • 35
  • 51
Gabriel Rebello
  • 1,077
  • 1
  • 11
  • 17
  • what you see the output when you print the products `print_r($products);` – Shaiful Islam Dec 28 '14 at 08:30
  • u can use the `->get()` just like normal and in the view chose the row u want by `$bla[0,1,2,etc..]->column_name` or use `->skip(how_many)->get()` but again this doesn't limit to a single row ,u will still need to process it even further in the view. – ctf0 Dec 28 '14 at 15:50

3 Answers3

2

You want to use take, limit the number of results to three and then print out every one with a foreach loop. Docs: Laravel Queries, see skip and take.

$products = DB::table('table2')->where('user_id',$user->id)->take(3)->get()

Then, inside your view, you can just iterate through this data:

@foreach($products as $p)

Alternatively, in your PHP you can iterate through this data using something like:

foreach ($products as $product) { var_dump($product); }

(You are getting that error because you are trying to output a result object as a whole, and not the data it contains. Using the loop actually fetches the data from the result object so you can then use the loop variable ($product) normally.)

Andrei Bârsan
  • 3,473
  • 2
  • 22
  • 46
  • That's it. As I noticed after reading Surya's and Marcin's answers, I was not echoing the data, but the object as a whole. And using `@foreach` solved it. By the way, the Laravel documentation doesn't explain what the argument inside `->take()` stands for. Is it like "take the three first objects?". Thanks for the help! I'm marking this as the solution since it explains in more detail than the other answers. – Gabriel Rebello Dec 28 '14 at 19:55
  • Yep. `take(n)` says *I'm only interested in the first `n` items*. Just like `skip(n)` says *I'm interested in everything except the first `n` items*. – Andrei Bârsan Dec 29 '14 at 13:52
1

To get data from database you can use one one those methods: all, get, or first.

Using all:

$products = DB::table('table2')->all();

you are getting all the products.

Using first you can use conditions but you will get only first record that fulfil conditions:

$products = DB::table('table2')->where('user_id',$user->id)->first();

Using get you can use conditions and you will get all the records that fulfil those conditions:

$products = DB::table('table2')->where('user_id',$user->id)->get();

So in your case you want to use get to get data from database.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

When you are using

$products = DB::table('table2')->where('user_id',$user->id);

Then $products is an array and you do not have to echo an array. To display the products you need to use a foreach loop like below

foreach ( $products as $key => $product ) {
   var_dump( $product );
}

If you want to show only three products, then you can use for loop inside foreach.

You can learn more about foreach from below link

http://php.net/manual/en/control-structures.foreach.php

Surya
  • 454
  • 6
  • 15
  • @ Down-voter: in Question @gabriel-rebello has written "I came up with the idea of getting each row separately and echoing them. For the first one, it's simple: $products->first(); but I have no idea on how to get the other two.". That's why I have written how to echo information separately using foreach loop. It will be good learning for me if you leave comment for the reason to down-vote. – Surya Dec 28 '14 at 15:01
  • Good! I didn't even think of **foreach**. Guess I still have a lot to learn. Querying with `->get();` to actually get an array of objects from the db and using `@foreach` on my view solved my problem. Thanks a lot Surya and Marcin! – Gabriel Rebello Dec 28 '14 at 18:11