A short reminder about my revisionable package (compatible with L4 & L5+) and quick example of how you can use it:
1. Controller:
1 2 3 4 5 6 7 8 |
public function history($id) { $ticket = Ticket::find($id); return view('tickets.revisions.timeline', compact('ticket')); } |
2. Model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
use Sofa\Revisionable\Revisionable; use Sofa\Revisionable\Laravel\RevisionableTrait; class Ticket extends Model implements Revisionable { use RevisionableTrait; protected $revisionPresenter = 'App\Presenters\Revisions\Ticket'; protected $revisionable = [ 'item_id', 'customer_id', 'status_id', 'responsible_id', 'defect', 'note', ]; // ... } |
3. Presenter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
use Sofa\Revisionable\Laravel\Presenter; class TicketPresenter extends Presenter { protected $labels = [ 'item_id' => 'Przedmiot', 'customer_id' => 'Klient', 'status_id' => 'Status', 'responsible_id' => 'Serwisant', 'defect' => 'Usterka', 'note' => 'Uwagi', ]; protected $passThrough = [ 'item_id' => 'item.name', 'customer_id' => 'customer.name', 'responsible_id' => 'serviceman.name', 'status_id' => 'status.name', ]; protected $actions = [ 'created' => 'utworzony', 'updated' => 'edytowany', 'deleted' => 'usunięty', 'restored' => 'przywrócony', ]; } |
4. Views (only relevant parts)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@unless (count($revisions)) <p>Nie znaleziono historii zmian dla podanych kryteriów</p> @else <section id="cd-timeline"> @foreach ($ticket->revisions as $revision) @include('revisions.single', ['revision' => $revision]) @endforeach </section> @endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<div class="alert alert-warning no-margin"> <caption> {{ $revision->created_at }} rekord <strong>{{ $revision->action }}</strong> przez: <strong>{{ $revision->user }}</strong>. </caption> </div> @if (count($revision->old)) <table class="table"> <thead> <tr> <th>Pole</th> <th>Stara wartość</th> <th>Nowa wartość</th> </tr> </thead> @foreach ($revision->old as $key => $v) <tr> <td>{{ $revision->label($key) }}</td> <td class="{{ $revision->isUpdated($key) ? ' danger' : '' }}">{{ $revision->old($key) }}</td> <td class="{{ $revision->isUpdated($key) ? ' success' : '' }}">{{ $revision->new($key) }}</td> </tr> @endforeach </table> @endif |
That’s it!
Any issues, please post to https://github.com/jarektkaczyk/revisionable/issues