Replies: 1 comment 3 replies
-
return $this->hasOne(Address::class)->where('usage', '=', 'billing') update
"Apparently it wrote the correct SQL but threw the error "must return a relationship instance but null was given." Null would be the correct return here because this is a new user with no addresses. I thought that the Eloquent queries were supposed to return null if no results were found." one to many should return empty collection not null. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to have a one of many relationship that selects the one based on the VALUE of a column, not on an aggregate. I don't want the latest or oldest or min or max, but the one that has a specific column value. For example, I have a many to many relationship between users and addresses, and for the users, I also want to have a one of many relationship called "billing address" where I select the address of the many with the "usage" of "billing". As far as I can see from the docs and trying to decipher the source code, the only way to do that is to run a phony min/max on some column then add a closure that does my real selection. What I want to be able to do is something like:
return $this->hasOne(Address::class)->ofMany('usage', '=', 'billing')
I have been unable to find a way to do that within the relationships system.
I tried the following that used my many-to-many relationship:
return $this->addresses()->where('addresses.usage','billing')
Apparently it wrote the correct SQL but threw the error "must return a relationship instance but null was given." Null would be the correct return here because this is a new user with no addresses. I thought that the Eloquent queries were supposed to return null if no results were found.
Beta Was this translation helpful? Give feedback.
All reactions