Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple database queries when using userResponsible #58

Open
max-online opened this issue Apr 25, 2014 · 11 comments
Open

Multiple database queries when using userResponsible #58

max-online opened this issue Apr 25, 2014 · 11 comments

Comments

@max-online
Copy link

Is there some way to eager load the username when using Revisionable to get the userResponsible for the revision?
(By the way: I am using Sentry)

@duellsy
Copy link
Member

duellsy commented Apr 25, 2014

currently no, open to pull requests on the dev branch for this though, sounds handy

@SuperlativeEntity
Copy link

+1

@mmodler
Copy link

mmodler commented Sep 24, 2014

+1

Why closed?

@duellsy
Copy link
Member

duellsy commented Sep 24, 2014

Closed since nobody decided it was important enough to work on, will re-open so someone can work on this and submit a pull request

@duellsy duellsy reopened this Sep 24, 2014
@mmodler
Copy link

mmodler commented Sep 25, 2014

My workaround for now in my Basemodel:

public function revisionHistoryWithUser()
{
    return $this->morphMany('\MyAppName\Models\Revision', 'revisionable')->with('user');
}

@carcinocron
Copy link

Is there a reason this doesn't work? (from Laravel docs)

$books = Book::all();

$books->load('author', 'publisher');

@warksit
Copy link

warksit commented Nov 27, 2014

Just to expand slightly on @mmodler response as it did lead me in the right direction.

Create a Revision class of your own extending \Venturecraft\Revisionable\Revision.
Add a true user relation:

    public function user(){
        return $this->belongsTo('User'); //Namespace as appropriate
    }

then add function revisionHistoryWithUser() as @mmodler said
If you already have your view set up remember to now reference user rather than userResponsible.

@InstanceOfMichael I don't believe that will work as userResponsible is not defined as a relationship of Revision.

@carcinocron
Copy link

That seems likely, I think defining that use relationship was probably one of the first things I did.

@pr4xx
Copy link

pr4xx commented Aug 2, 2017

+1

1 similar comment
@mavci
Copy link

mavci commented Mar 18, 2019

+1

@cuberinooo
Copy link

as @warksit pointed it out. You can make it more simple just by using the relation.

Extends Revision and put the following code:

public function user(){ return $this->belongsTo(User::class); }

In your application you can use it like $revision->user instead of $revision->userResponsible(). And just as relations you can do your eager load as you would do with eloquent models.
Btw if you load your revisions: do it like Revision::with(['revisionable','user'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants