Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lifesound's avatar

Pass collection to view

How to pass collection to view ??

Or get collection inside view ??

0 likes
27 replies
willvincent's avatar
Level 54
$users = User::all();

return view('users')->with(compact('users'));
2 likes
d3xt3r's avatar

Did you even try? What the issue/problem you are facing ?

1 like
jlrdw's avatar

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.

1 like
lifesound's avatar

when you pass a var with compact , it will be array not collection … what I'm asking about is passing the collection as collection

1 like
d3xt3r's avatar

@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. 
1 like
d3xt3r's avatar

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

1 like
lifesound's avatar

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

1 like
lifesound's avatar

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

1 like
d3xt3r's avatar

When you get back to the lab, tell us exact error, and exact implementation(whatever you have done). Cannot guess..

1 like
jlrdw's avatar

@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

1 like
zachleigh's avatar

@jlrdw A collection is an object. It can act like an array, but it is an object.

2 likes
d3xt3r's avatar

@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 ?

1 like
Snapey's avatar

@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.

1 like
lifesound's avatar

All i need to use this arrow -> not this [] . I will test again and tell

1 like
Snapey's avatar

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

1 like
jlrdw's avatar

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.

1 like
d3xt3r's avatar

@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.

1 like
zachleigh's avatar

@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.

1 like
Snapey's avatar

@d3xt3r

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)

1 like
d3xt3r's avatar

@Snapey You can :) Give it a try, as i said IteratorAggregate lets you chose how you want to iterate over it, but even without it you can use foreach..

By default, all visible properties will be used for the iteration.

Source

2 likes
jlrdw's avatar
$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
        )

)
1 like
lifesound's avatar

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.