VarboxInternational / varbox

THE Laravel Admin Panel package built by developers, for developers
https://varbox.io
Other
63 stars 13 forks source link

Extend Revision model #14

Closed achancu closed 3 years ago

achancu commented 4 years ago

What are you trying to achieve (screenshots might help):

I’m using the HasRevisions trait on my Article custom entity and everything works as expected.

However, recently I’ve extended the revisions database table to also support soft deleting by adding a deleted_at column, but I don’t know how to actually extend the Revision model to support soft deleting.

Can someone please help?

Thanks a lot!

zbiller commented 4 years ago

As with most custom developed functionalities inside Varbox, you have the option of overwriting the bindings.
This means that you can swap the Varbox\Models\Revision model with your own implementation and Varbox will take care of using your implementation everywhere.

To overwrite the revision model binding, go to config/varbox/bindings.php and change the value for revision_model:

/*
|
| Concrete implementation for the "revision model".
| To extend or replace this functionality, change the value below with your full "revision model" FQN.
|
| Your class will have to (first option is recommended):
| - extend the "Varbox\Models\Revision" class
| - or at least implement the "Varbox\Contracts\RevisionModelContract" interface.
|
| Regardless of the concrete implementation below, you can still use it like:
| - app('revision.model') OR app('\Varbox\Contracts\RevisionModelContract')
| - or you could even use your own class as a direct implementation
|
*/

'revision_model' => \App\YourRevisionModel::class,

// 'revision_model' => \Varbox\Models\Revision::class,

Once that's done, you can write your soft deleting behavior inside your model.

The easiest way to conform with the requirements of the revision model is to extend your own model with the Varbox one, or at least implement the Varbox\Contracts\RevisionModelContract

<?php

namespace App;

use Illuminate\Database\Eloquent\SoftDeletes;
use Varbox\Models\Revision as VarboxRevisionModel;

class YourRevisionModel extends VarboxRevisionModel
{
     use SoftDeletes;
}

Here's the documentation part for reference: https://varbox.io/docs/1.x/model-revisions#overwrite-bindings

Also, you might want to learn about overwriting bindings: https://varbox.io/docs/1.x/custom-bindings