1

Using Laravel 5.4 and the VentureCraft/revisionable package.

I have 3 models: User, Company and Order.

The Order model implements the Revisionable trait:

namespace App\Models;

class Order extends Eloquent {
    use \Venturecraft\Revisionable\RevisionableTrait;

    // ...
}

The User model has a relationship with the Company model:

public function company() {
    return $this->belongsTo('App\Models\Company');
}

And I would like to eager load the company of a user from an order revision. Something like this

$order = Order::with('revisionHistory.user.company')->find(1);

foreach($order->revisionHistory as $revision) {
    $company = $revision->user->company;
}

I've tried overwriting various methods (e.g. revisionHistory and identifiableName) from the Revisionable trait without any luck.

Camilo
  • 6,504
  • 4
  • 39
  • 60
  • not each revision history is for company_id field, you may filter out them first, and get company by id – Hanlin Wang Sep 27 '17 at 10:13
  • Maybe I wasn't clear. I'm not looking for changes in the `company_id` field. I'm trying to get the `Company` of the use who made the change in the `Order` model. All of the users has a company related. – Camilo Sep 27 '17 at 15:24
  • 1
    understand! just answered. – Hanlin Wang Sep 27 '17 at 15:28

2 Answers2

1

You can use $company = $revision->userResponsible()->company to get company of the User that change the Order.

Hanlin Wang
  • 779
  • 4
  • 17
1

I think this is not possible with the current version of the package.

The reason behind is that userResponsible() is not an actual relationship, it's just a function that returns an instance of the User model.

To allow eager loading, something like this would be needed:

public function userResponsible()
{
    if (class_exists($class = '\Cartalyst\Sentry\Facades\Laravel\Sentry')) {
        return $this->belongsTo(Config::get('sentry::users')['model'], 'user_id');
    } else if (class_exists($class = '\Cartalyst\Sentinel\Laravel\Facades\Sentinel')) {
        return $this->belongsTo(Config::get('sentinel::users')['model'], 'user_id');
    } else {
        return $this->belongsTo(Config::get('auth.model'), 'user_id');
    }
}

Then you would be able to eager load like this:

$order = Order::with('revisionHistory.userResponsible.company')->find(1);

You can view the original issue in GitHub.

Camilo
  • 6,504
  • 4
  • 39
  • 60