Reputation: 157
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 ?
Upvotes: 0
Views: 1396
Reputation: 5166
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
Upvotes: 0
Reputation: 11
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
Upvotes: 0