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

Request to allow eager load for old_value and new_value #373

Open
jrbecart opened this issue Mar 23, 2020 · 0 comments
Open

Request to allow eager load for old_value and new_value #373

jrbecart opened this issue Mar 23, 2020 · 0 comments

Comments

@jrbecart
Copy link

jrbecart commented Mar 23, 2020

I have a page that display all the changes made by a user.
For this use case I added the following enhancement to speed up considerably my revision page.

I created a model extending \Venturecraft\Revisionable\Revision and added some relation like:

public function old_relation_a()
{
    return $this->belongsTo('App\Models\Relation_a','old_value');
}

public function new_relation_a()
{
    return $this->belongsTo('App\Models\Relation_a','new_value');
}

And in GetValue function from \Venturecraft\Revisionable\Revision.php I added this:

+ $item = ($this->relations[$which . "_" . $related_model] ?? null);
                    
+ if(!$item)
+ {
  $related_class = $main_model->$related_model()->getRelated();

  // Finally, now that we know the namespace of the related model
  // we can load it, to find the information we so desire
  $item = $related_class::find($this->$which_value);
+ }

Finally I load my revisions like this:

// Example 
$res = \App\Models\MyRevision::where('user_id', $user->id)->get();
$res->where('revisionable_type', 'App\Models\User')->load('user'); // eager load user model
$res->where('key', 'user_type_id')->load('old_user_type'); // eager load related model (like old_relation_a)
$res->where('key', 'user_type_id')->load('new_user_type'); // eager load related model (like new_relation_a)

This way I can speed up my load page and decrease considerably the number of queries.
I will be great to add it, let me know if you want this and I can create a PR.
(I actually have this change in my model, but I have to reuse most of the parent method)

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

No branches or pull requests

1 participant