How do you check if a relation is defined?
See original GitHub issueI have an User and a Cart model like this
class User extends Model {
static boot() {
super.boot();
this.addGlobalScope(builder => builder.with('cart'));
}
cart() {
return this.hasOne('App/Models/Cart');
}
}
class Cart extends Model {
user() {
return this.belongsTo('App/Models/User');
}
}
now I want to check if a user has a cart (there’s no cart_id on user with this relation).
// this gets me the "cart" relation as well because of the global scope.
const user = await auth.getUser();
if (user.cart) {
...
}
how to I check if the user has an associated cart without fetching again?
Boolean(user.cart) // always true, even if the user has no cart
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
How Do You Figure Out If a Relation is a Function? | Virtual Nerd
Watch this tutorial to see how you can determine if a relation is a function. ... Learn the definition of a function and...
Read more >How to Determine Whether the Relation is a Function
A relation is a function if it relates every element in its domain to one and only one element in the range.
Read more >Relations and Determining Whether a Relation is a Function
Use the vertical line test to determine whether or not a graph represents a function. If a vertical line is moved across the...
Read more >1.2 Determining Whether a Relation Represents a Function
A relation is a set of ordered pairs. The set of the first components of each ordered pair is called the domain and...
Read more >Relations and functions (video) - Khan Academy
So in a relation, you have a set of numbers that you can kind of view as the input into the relation. We...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you have eagerloaded a relationship, then you can check it as.
I think the question is still valid. Maybe you wanna remove the “invalid” tag so someone else can benefit from the answer.