5

In the revisionable package it's possible to get the identifiable name of a related model by defining the identifiableName() method on a model, setting the field to display instead of id. E.g. 'name' or 'title'.

Is it possible to do similar with unrelated model. So say I got all revisions for a certain model. E.g.

$revisions = Venturecraft\Revisionable\Revision::where('revisionable_type', 'post')->get();

How can I get the identifiableName of the post for which each revision was made?

I can already get the post id with $revision->revisionable_id but I can't seem to find a way to get the post name without something like Post::find($revision->revisionable_id)->name which is expensive for a large list of revisions.

harryg
  • 23,311
  • 45
  • 125
  • 198

1 Answers1

0

For anyone still having this issue.

I think you may be able to do something like:

$revision->revisionable->identifiableName();

Works fine for me, just be sure to check that the post hasn't been deleted, otherwise you will be trying to get a property from a non-object.

eg:

if($revision->revisionable){
   // Post exists
   $revision->revisionable->identifiableName();
}else{
   // Post doesn't exist
}