Getting the model "identifiableName ()" from the revised

In the checked package, you can get the identifiable name of the associated model by defining the identifiableName() method on the model, setting the display field instead of I would. For instance. 'name' or 'title'.

Is it possible to do this with an unrelated model. So say that I have all versions for a specific model. For instance.

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

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

I can already get the message identifier with $revision->revisionable_id , but I cannot find a way to get the message name without something like Post::find($revision->revisionable_id)->name , which is expensive for a large list of fixes.

+5
source share
1 answer

For everyone who has this problem.

I think you can do something like:

 $revision->revisionable->identifiableName(); 

Works great for me, just make sure the message is not deleted, otherwise you will try to get the property from a non-object.

eg:

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

Source: https://habr.com/ru/post/1206442/


All Articles