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

SigalZ's avatar

Laravel collection creation

Using Laravel 8 I create a collection:

$origins = collect();

$x = GreenBean::whereHas('coffees', function($q) {
    $q->where('active', 1);
})->get();

foreach($x as $y) {
    if(!$origins->contains('name', $y->country->origin->name))
        $origins->push(['name'=>$y->country->origin->name]);
}

When I try to get a value like this:

foreach($origins as $origin) {
    dd($origin->name);
}

I get the error: Trying to get property 'name' of non-object but this works:

foreach($origins as $origin) {
    dd($origin['name']);
}

Why is that?

0 likes
33 replies
Sinnbeck's avatar

You are pushing arrays into it so it isn't an object

This is an array

['name'=>$y->country->origin->name]
SigalZ's avatar

@Sinnbeck So how do I make it a collection? How do I add items as collection?

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@SigalZ the foreach part is a collection. Not the items in the collection. It sounds like you want them as objects?

You can try

$origins->push((object) ['name'=>$y->country->origin->name]);
1 like
SigalZ's avatar

@Sinnbeck Yep, that works! At last, I know how to create collections :) Thank you!

tykus's avatar

What are you actually trying to get as a result here - you are probably approaching the problem incorrectly???

tykus's avatar

@SigalZ of what; what should it look like whenever the operation is complete???

SigalZ's avatar

@tykus I want a collection that holds origins,

inside origins there will be countries,

inside the countries there will be beans

and I want to reach each value with -> not [''] like the collections I get back from Eloquent queries for example.

e.g.

@foreach($origins as $origin) {
	<p>{{ $origin->name }}</p>
@foreach($origins->countries as $country)
	<p>{{ $country->name}}</p>
@foreach($countries->beans as $bean)
	<p>{{ $bean->name }}
@endforeach
etc.
1 like
webrobert's avatar

@SigalZ this is in part why to solve this with relations over joins. It cascades down into the presentation layer. IF you can stay within the relationship/model your blade files can be better unified.

tykus's avatar

@SigalZ what is $origins; just an arbitrary Collection?

Are you wanting nested loops here??? How does $origins get $countries???

@foreach($origins as $origin) {
    <p>{{ $origin['name'] }}</p>
    @foreach($origin->countries as $country)
        <p>{{ $country->name}}</p>
        @foreach($country->beans as $bean)
	        <p>{{ $bean->name }}
        @endforeach
    @endforeach
@endforeach
SigalZ's avatar

@webrobert But we could not make it work with relationship/model. So now I'm trying a different way

Sinnbeck's avatar

@SigalZ perhaps you should make a test repo with the migrations/models and the failing query. I have never had a problem with exists like the examples made by @webrobert

SigalZ's avatar

@Sinnbeck thank you but I don't use migrations. I create my tables manually. I also ran all these queries in phpMyAdmin and they all return the same results. Not sure what test repo is?

SigalZ's avatar

@tykus I'm playing around to see how to create collections.

I have these 4 models: origin, country, green_bean, coffee

origin has many countries

country has many grean_beans

grean_beans have many coffees.

In another post here (https://laracasts.com/discuss/channels/laravel/laravel-collection-creation?page=1&replyId=772054) I was trying to get only origins/countries/green beans that have Active coffees in them with no success.

So now I'm trying a different way and for once, I want to understand how to create a collection not arrays.

$origins = collect();
$countries = collect();
$beans = collect();

$x = GreenBean::whereHas('coffees', function($q) {
    $q->where('active', 1);
})->get();
tykus's avatar

@SigalZ you need to constrain the eager-loaded relations too

$constraint = fn ($builder) => $builder->where('active', 1);
$origins = Origin::with(['countries.green_beans.coffees' => $constraint])
    ->whereHas('countries.green_beans.coffees', $constraint)
    ->get();
SigalZ's avatar

@tykus Thx, this still gives me countries that do not have active coffees in them.

The sql it creates:

▼
    "query" => "select * from `origins` where exists (select * from `countries` where `origins`.`id` = `countries`.`origin_id` and exists (select * from `green_beans` where `countries`.`id` = `green_beans`.`country_id` and exists (select * from `coffees` where `green_beans`.`id` = `coffees`.`green_bean_id` and `active` = ?) and `green_beans`.`deleted_at` is null) and `countries`.`deleted_at` is null) and `origins`.`deleted_at` is null
MohamedTammam's avatar

I don't really know why you are trying to use object syntax instead of array.

But here's how you can achieve this.

foreach($x as $y) {
    if(!$origins->contains('name', $y->country->origin->name))
        $origins->push(['name'=>$y->country->origin->name]);
}
$origins->map(function($arr){ return (object) $arr;});
SigalZ's avatar

@MohamedTammam Thank you. I want to do it because I prefer that syntax and I'm simply curious of how to do it.

Your solution gives me the same error:

Trying to get property 'name' of non-object

Sinnbeck's avatar

@SigalZ I have given you an e example above. But I still suggest working more on eloquent

SigalZ's avatar

@sinnbeck @webrobert I would have loved to use Eloquent, if I could just make it work...

Very frustrating. I can't make it work with or without it.

I appreciate all your help, you guys are amazing.

SigalZ's avatar

@Sinnbeck Hi, I do not use migrations. Will it be ok if I upload database export? Will you need anything else beside the models?

If you go to https://highlandcoffee.co.za and click on 'Our Coffees' menu item, you will see what I need to display. So just to recap, I need to show Origins in them Countries-Green Bean Name- Only active Coffees that belong to these beans

So if there is an origin that does not have active coffees down the line, it should not be displayed.

Sinnbeck's avatar

@SigalZ yeah I get what you want. If you have a demo repo (on github) with the models copied over and a dump of the database, then that's perfect

Sinnbeck's avatar

@SigalZ Awesome. I tried the raw query from the other thread, and got no Peru..

Please or to participate in this conversation.