PHP code example of laragear / rewind

1. Go to this page and download the library: Download laragear/rewind library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laragear / rewind example snippets


use App\Models\Article;

$article = Article::find(1);

$article->rewind()->toLatest();

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laragear\Rewind\HasRewind;

class Article extends Model
{
    use HasRewind;
    
    // ...
}

use App\Models\Article;

$article = Article::find(1);

$article->rewind()->toLatest();

use App\Models\Article;
use Illuminate\Http\Request;

public function store(Request $request)
{
    $validated = $request->validate([
        // ...
    ]);
    
    return Article::create($validated); // First state created automatically.
}

public function update(Article $article, Request $request)
{
    $validated = $request->validate([
        // ...
    ]);
    
    $article->update($validated); // State pushed automatically.
    
    return $article;
}

use App\Models\Article;
use Illuminate\Http\Request;

public function store(Request $request)
{
    $validated = $request->validate([
        // ...
    ]);
    
    // Create an article but don't save the first state.
    return Article::withoutCreatingStates(fn() => Article::create($validated));
}

use App\Models\Article;
use Illuminate\Http\Request;

public function store(Request $request)
{
    $validated = $request->validate([
        // ...
    ]);
    
    $article = Article::create($validated);
    
    $article->rewind()->create(); // Save the first state.
    
    return $article;
}

public function update(Article $article, Request $request)
{
    $validated = $request->validate([
        // ...
    ]);
    
    $article->update($validated);
    
    $article->rewind()->create(); // Push a new state.
    
    return $article;
}

$article->rewind()->create(
    keep: true,
    prune: false,
);

public function draft(Article $article, Request $request)
{
    $validated = $request->validate([
        // ...
    ]);
    
    // Push a state from the updated article without persisting it.
    $article->fill($validated)->rewind()->create();
    
    return back();
}

use App\Models\Article;

$pastArticles = Article::find(1)->rewind()->all();

use App\Models\Article;

$count = Article::find(1)->rewind()->count();

use App\Models\Article;

$hasAtLeastOneState = Article::find(1)->rewind()->exists();

use App\Models\Article;

$hasNoStates = Article::find(1)->rewind()->missing();

use App\Models\Article;

$latest = Article::find(1)->rewind()->getLatest();

$oldest = Article::find(1)->rewind()->getOldest();

use App\Models\Article;

$pastArticle = Article::find(1)->rewind()->find(456);

use App\Models\Article;

$article = Article::find(1);

$article->title; // "Happy cavations!"

$article->rewind()->to(468)->save();

$article->title; // "Happy vacations!"

use App\Models\Article;

Article::find(1)->rewind()->toLatest()->save();

Article::find(1)->rewind()->toOldest()->save();

use App\Models\Article;

$stale = Article::find(1);

$stale->title; // "Happy vatacions!"

$past = $original->rewind()->getLatest();

$past->save();

$stale->title; // "Happy vatacions!"

$stale->fresh()->title; // "Happy vacations!"

use App\Models\Article;

Article::find(1)->rewind()->remove(765);

use App\Models\Article;

$article = Article::find(1);

$article->rewind()->removeLatest();
$article->rewind()->removeOldest();

> Article::find(3)->rewind()->deleteOldest(true);
> 

use App\Models\Article;

Article::find(1)->rewind()->clear();

use App\Models\Article;

Article::find(1)->rewind()->forceClear();

use App\Models\Article;

Article::find(1)->rewind()->prune();

use Laragear\Rewind\Events\StateCreated;

class MyListener
{
    public function handle(StateCreated $event)
    {
        $event->model; // The target model.
        $event->state; // The RewindState Model with the data. 
    }
}

use App\Models\Article;
use Illuminate\Support\Facades\Route;
use Laragear\Rewind\Events\StateRestored;

Route::post('article/{article}/restore', function (Article $article) {
    $article->rewind()->toOldest()->save();
    
    StateRestored::dispatch($article);
    
    return back();
});

use App\Models\Article;

$page = Article::find(1)->rewind()->query()->paginate();

use Laragear\Rewind\Models\RewindState;

RewindState::query()->where('created_at', '<', now()->subMonth())->delete();

public function rewindLimit()
{
    return 10;
}

public function rewindLimit()
{
    return now()->subMonth();
}

public function rewindLimit()
{
    return [10, now()->subMonth()];
}

public function rewindLimit()
{
    return false;
}

public function shouldCreateRewindStateOnCreated(): bool
{
    // Don't save states if the article body is below a certain threshold.
    return strlen($this->body) < 500;
}

public function shouldCreateRewindStateOnUpdated(): bool
{
    // Only let subscribed users to have states saved. 
    return $this->user->hasSubscription();
}

public function shouldPruneOldRewindStatesOnUpdated(): bool
{
    return true;
}

public function shouldKeepFirstRewindState(): bool
{
    return true;
}

use Illuminate\Contracts\Support\Arrayable;

public function getAttributesForRewindState(): Arrayable|array
{
    return [
        'author_id' => $this->author_id,
        'title' => $this->title,
        'slug' => $this->slug,
        'body' => $this->body,
        'private_notes' => null,
        'published_at' => $this->published_at,
     ];
}

use Illuminate\Support\Arr;

public function setAttributesFromRewindState(array $attributes): void
{
    $this->setRawAttributes(Arr::except($attributes, 'excerpt'));
}
shell
php artisan vendor:publish --provider="Laragear\Rewind\RewindServiceProvider" --tag="migrations"
shell
php artisan migrate