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

Trocamedo's avatar

How to check if a collection from a collection is empty (Query Builder?)

I am trying to see if $buyer->membership_number has a value or if it is empty. $buyer being a collection. If I dd($buyer->membership_number) I get another collection, named Value. How do I check what/if the second collection returns what I want or not? I can't use isEmpty() because it is the query builder that I am using, if I understand it correctly.

if($buyerIndex === 1 && !$buyer->membership_number->isEmpty()) {
	$row = array_merge($row, $this->mapBuyer($buyer));
}

This is pretty much what I want to do but can't.

I don't fully understand how if I want it to display something $buyer->membership_number works but if I try to use it in a if-else statement it doesn't work as when I display it. I use laravel 5.5.

0 likes
6 replies
tykus's avatar

$buyer being a collection

dd($buyer->membership_number) I get another collection, named Value

I can't use isEmpty() because it is the query builder that I am using

It is very difficult to understand what you are working with here - why does a Collection have a membership_number property. How is the Collection named Value? Where are you working with the Query Builder?

Trocamedo's avatar

$buyer being an Eloquent model named residenceBuyer, returns a collection when dd()

$buyer->membership_number also being an Eloquent model named Value, returns a collection when dd()

!$buyer->membership_number->isEmpty() gives me an error page saying isEmpty() is an undefined method in Illuminate\Database\Query\Builder::isEmpty() I am outputting this to an exporter for excel-sheet but query builder issues comes up in my if-statement.

I have taken over a laravel project which I have never worked on before, at the same time not worked with php and laravel before. So if more information is needed just say the word as I don't really know what is needed.

tykus's avatar

@Trocamedo how are you getting the $buyer variable - can you share the Eloquent query where it is assigned?

Trocamedo's avatar

@tykus

protected $project;

public function setup(Project $project)
{
        $this->project = $project;
}

public function data(): array {
foreach ($this->project->residences as $residenceIndex => $residence) {
            if ($residence->buyers->count() > 0) {
                foreach ($residence->residenceBuyers as $buyerIndex => $buyer) {
				...my if-statement
				}
.....
}

Project is a class that extends an abstract class named ItemModel which is extending BaseModel which is an Eloquent\Model and extending Model which is in namespace Database\Eloquent. I don't think I can share the eloquent query, can't use toSql() on it. Residence are the same as project regarding class.

I dd($buyer->membership_number) and looked for where the membership id is supposed to be, it was named value so did $buyer->membership_number->value and I got my variable to use in my if-statement. It feels like a dirty way to do it though.

tykus's avatar

@Trocamedo is it $residence->buyers or $residence->residenceBuyers?

foreach ($this->project->residences as $residenceIndex => $residence) {
    if ($residence->buyers->count() > 0) {
        foreach ($residence->residenceBuyers as $buyerIndex => $buyer) {
			// Why is $buyer a Collection (and not a Model instance) here????
        }

What is the relationship between Residence and Buyer???

Trocamedo's avatar

@tykus it's both. $residence->buyers is an actual Collection. residenceBuyers is an Eloquent model like residence.

A residenceBuyer buys the Residence, which can have several residenceBuyers. A buyer is the residenceBuyer, which is an Eloquent model.

They are Models but return a Collection when dd()? https://laravel.com/docs/5.5/eloquent#collections I believe I am looping over the collection which the model returns?

Please or to participate in this conversation.