$users = User::all();
return view('users')->with(compact('users'));
Pass collection to view
How to pass collection to view ??
Or get collection inside view ??
This will be array not collection
Did you even try? What the issue/problem you are facing ?
Query results is always an array in one form or the other. That is a big drawback, you can't visit one record at a time like using a cursor. Mysql has a cursor, but not as efficient as sql server.
when you pass a var with compact , it will be array not collection … what I'm asking about is passing the collection as collection
Have you checked out laravel-recipes http://laravel-recipes.com/recipes/239/rendering-a-view-for-items-in-a-collection
@lifesound Objection!!! Prejudice. Am revising Suits, can't help it.
when you pass a var with compact , it will be array not collection,
The data will be constructed as array, what you need it the first place, but the underlying value is preserved, it not converted into array. Having said that
$users = User::all();
return view('users')->with(compact('users'));
// When you use $users in view it will be a collection and not array.
A collection is an array. https://laravel.com/docs/5.2/collections#introduction The Illuminate\Support\Collection class provides a fluent, convenient wrapper for working with arrays of data. The data is in an array, collection is a wrapper for working with the data in the array.
A collection is an array
I don't know what to say, am bewildered :(
@jlrdw Try this simple example
$arr1 = User::all();
$arr2 = User::all();
dd(array_merge($arr1, $arr2));
On the face of it, it says
array_merge(): Argument #1 is not an array
Again when i pass data with compact , I can't use collection methods like
foreach ($users as $user) {
echo $user->name;
}
give me an error
What error ??
I m not on my lab now, the error tell that you use object with array ..
telling that is an array not object
so i have to use toAreay()
When you get back to the lab, tell us exact error, and exact implementation(whatever you have done). Cannot guess..
@d3xt3r I guess you are the type that would argue with a brick wall. I guess you figure the helz with even trying laravel recipes, that has excellent tutorials. This
The Illuminate\Support\Collection class provides a fluent, convenient wrapper for working with arrays of data.
is from @TaylorOtwell not me, why in the Fucz do you want to argue with me about the way Taylor defined something. fucz purposely mis-spelled.
arrays of data
@jlrdw A collection is an object. It can act like an array, but it is an object.
@jlrdw Damn right, but only if pushed against them :) Nowhere, he mentioned that its an array, rather its a wrapper to work with arrays. Guess two things are different, ain't they ?
@jlrdw A collection is an object. It can act like an array, but it is an object.
and of course you can foreach on it because it presents an arrayable interface.
All i need to use this arrow -> not this [] . I will test again and tell
Show some code. What gets passed is a collection object, and will contain an array of other objects.
If you query the data correctly then its perfectly possible to do this;
@foreach($users as $user)
<li>{{ $user->name }}</li>
@endforeach
An object is also an array, just different type. All resultsets in mysql are arrays in one form or the other. Or list, which is an array, object is derived from notation, nothing more.
@Snapey Minor correction
and of course you can foreach on it because it presents an arrayable interface.
It does not have to do with Arrayable interface, PHP's foreach works on both arrays and objects, you may further implement IteratorAggregate if you want foreach to behave in a specific way.
@jlrdw I think i will call it the day. But will leave you with last question, can you run all native php array operations on collections ? If yes, i will agree that its an array, or else, will have to say, collections can BEHAVE LIKE array to some extent.
@jlrdw Talk about arguing with a brick wall....
$collection = collect([1,2,3,4]);
dd(gettype($collection));
// "object"
If you want to believe its an array, be my guest.
probably beyond my simplistic mind, but you can only iterate over an object contents if it implements specific features;
class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable
clearly you cannot for each on a single user because it probably does not contain iterable data (even though its an object)
$names = $query->fetchAll(PDO::FETCH_OBJ);
echo "<pre>";
print_r($names);
echo "</pre>";
Array
(
[0] => stdClass Object
(
[dogname] => Bridget
)
[1] => stdClass Object
(
[dogname] => Chrissy
)
[2] => stdClass Object
(
[dogname] => Bently
)
[3] => stdClass Object
(
[dogname] => Tom Sawyer
)
[4] => stdClass Object
(
[dogname] => Charlie
)
[5] => stdClass Object
(
[dogname] => Ariana
)
$names = $query->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($names);
echo "</pre>";
Array
(
[0] => Array
(
[dogname] => Bridget
)
[1] => Array
(
[dogname] => Chrissy
)
[2] => Array
(
[dogname] => Bently
)
[3] => Array
(
[dogname] => Tom Sawyer
)
[4] => Array
(
[dogname] => Charlie
)
[5] => Array
(
[dogname] => Ariana
)
)
@jlrdw And then Laravel wraps that in a collection.
I tested it .. worked
So the question is very basic
i disappointed ..
i will give the answer to the first one ensure that fact
Please or to participate in this conversation.