You are pushing arrays into it so it isn't an object
This is an array
['name'=>$y->country->origin->name]
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
You are pushing arrays into it so it isn't an object
This is an array
['name'=>$y->country->origin->name]
@Sinnbeck So how do I make it a collection? How do I add items as collection?
@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]);
@Sinnbeck Thank you. I will try that tomorrow. Had enough for today ;)
@Sinnbeck Yep, that works! At last, I know how to create collections :) Thank you!
What are you actually trying to get as a result here - you are probably approaching the problem incorrectly???
@tykus I am trying to create multi level collection
@SigalZ you seem to confuse collections with classes/objects
@SigalZ of what; what should it look like whenever the operation is complete???
@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.
@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.
@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
@webrobert But we could not make it work with relationship/model. So now I'm trying a different way
@webrobert oh... right...
@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
@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?
@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();
@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();
@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
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;});
@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
@SigalZ I have given you an e example above. But I still suggest working more on eloquent
@SigalZ there is a highly recommended free series here Laravel 8 from Scratch.
Also in the top five pro tips, “don’t fight the framework”
@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 always happy to help. If we can recreate it, maybe we can help fix it. Perhaps you can try running an export of your migrations and make a simple github repo with the models + Schema needed
https://laravel.com/docs/9.x/migrations#squashing-migrations
It is just a suggestion, so you don't have to do it if you don't want to. Just saying it might make things easier
@SigalZ did you try what I suggested?
https://laracasts.com/discuss/channels/laravel/laravel-collection-creation?page=1&replyId=772068
@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.
@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 Ok, I created it. I'm going away on Monday and a bit busy preparing things so might not be around a lot, so please bear with me if I don't see your comments quickly.
@SigalZ cool. Can you post the query that isn't working as well?
@Sinnbeck Added the file: TriedQueries.txt
@SigalZ Awesome. I tried the raw query from the other thread, and got no Peru..
Please or to participate in this conversation.