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