GithubHelp home page GithubHelp logo

n+1 problem about laravel-follow HOT 10 CLOSED

overtrue avatar overtrue commented on May 31, 2024
n+1 problem

from laravel-follow.

Comments (10)

overtrue avatar overtrue commented on May 31, 2024 2

OK, I am working on this.

from laravel-follow.

Livijn avatar Livijn commented on May 31, 2024

Something like this should work (haven't tried):

public function hasFavorited($target, $class = __CLASS__)
 {
    return $this->relationLoaded('favorites') 
        ? $this->favorites->where('pivot.followable_id', $target->id)
            ->where('followable_model', get_class($target))
            ->count()
        : Follow::isRelationExists($this, 'favorites', $target, $class);
}

from laravel-follow.

xsaad67 avatar xsaad67 commented on May 31, 2024

Is there any update on eager loading?

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

@Livijn @xsaad67 Fixed.

from laravel-follow.

xsaad67 avatar xsaad67 commented on May 31, 2024

Hy thanks for the response but how do I call the upvoters relationship on eager loading? Like Post::with("upvoters")->get(); ?

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

yes

from laravel-follow.

rdgout avatar rdgout commented on May 31, 2024

Hi @overtrue

When I use the loaded relation the morph gets a User model. However the relationKey checks for user_id whilst the User model usually contains a id field.
That way, the entire new if check never gets any results.

Can you fix this?

Reproducing is easy:

  1. Create 2 user models
  2. Follow user 1 from user 2
  3. Follow user 2 from user 1
  4. Fetch user 2 whilst eager loading the followings and followers
  5. Check the result for isFollowedBy and isFollowing

Both should be false since the if checks already results in an empty collection.
if the relationKey is 'id' it does work.

        if ($model->relationLoaded($relation)) {
            $relationKey = 'id';

            return $model->{$relation}->where($relationKey, head($target->ids))->isNotEmpty();
        }

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

@rdgout I will check this.

from laravel-follow.

rdgout avatar rdgout commented on May 31, 2024

@overtrue

Issue 76 has the same issue I found.

from laravel-follow.

overtrue avatar overtrue commented on May 31, 2024

@rdgout I've fixed the problem. plz take a try with new version.

from laravel-follow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.