Reputation:
I have a function in the model and I return it to a variable like the following
$books = DB::select('select * from books where category_id = 2');
return $books;
And Laravel Query Builder return an object.How I can convert it to the string from the controller? Have a function for that? Please let's me know.
Upvotes: 3
Views: 34801
Reputation: 87719
What you get from this query
$books = DB::select('select * from books where category_id = 2');
If you do
var_dump($books);
You'll see that it's an array, not an object.
You need a big string with all your records? You can serialize it:
return serialize($books);
But you'll have to
unserialize($value);
to use it.
You need to use the values you get from that select? You can use foreach()
on that array:
foreach($books as $book)
{
echo $book['name'];
}
Or you can do your select differently:
$books = Book::where('category_id',2)->get();
And do your foreach using the object notation:
foreach($books as $book)
{
echo $book->name;
}
Upvotes: 10