question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you check if a relation is defined?

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
thetutlagecommented, May 21, 2018

If you have eagerloaded a relationship, then you can check it as.

if (user.getRelated('cart')) {
}
1reaction
somonekcommented, May 21, 2018

I think the question is still valid. Maybe you wanna remove the “invalid” tag so someone else can benefit from the answer.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found