简体   繁体   中英

How to use VentureCraft/revisionable in Laravel?

I want know how to use VentureCraft/revisionable in CRUD application and get a history

Ex : users every day need to add mileage and edit mileage ...... in my view (revision.blade.php) i want get history who has add and edit i tried but i don't know how to go next step ?

This is Model

class Mileage extends Model
{
    use \Venturecraft\Revisionable\RevisionableTrait;
    protected $guarded = [];
}

This is Route

Route::get('/history', function () {
    $mileage = \App\Mileage::all();
    $history = $mileage->revisionHistory;
    return view('revision',compact('history'));
});

i don't know what code to use for view ????

mileage has user_id, date,mileage i want show like this

Ex : user ...A... has add mileage for .....date.... value is ...mileage...

if they edit i want show like this user ...A... has update mileage for .....date.... value is ...mileage... to ...mileage... how can i do this ?

Before displaying the history. Firstly the revisionable library assumes that in your Mileage model the relationship method for the user model is called user() because the FK in the milage table is user_id. Also you need to add the following code to your User model and Mileage model.

public function identifiableName(){
    return $this->name;
}

On your view you need to loop through the history array like this.

@foreach($history as $h )
     <li>{{ $h->userResponsible()->first_name }} has updated milage  {{ $h->fieldName() }} from {{ $h->oldValue() }} to {{ $h->newValue() }}</li>
@endforeach
Route::get('/history', function () {
    $mileage = \App\Mileage::all();
    return view('revision',compact('mileage'));
});

Now in your blade file

  @foreach($mileage->revisionHistory as $history )
    <p class="history">
      {{ $history->created_at->diffForHumans() }}, {{ $history->userResponsible()->name }} changed
      <strong>{{ $history->fieldName() }}</strong> from
      <code>{{ $history->oldValue() }}</code> to <code>{{ $history->newValue() }}</code>
    </p>
  @endforeach

Now if you need history in controller or in any other place you have to call revisionHistory of instances of model that uses RevisionableTrait trait

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM