1. Go to this page and download the library: Download iben12/laravel-statable 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/ */
namespace App;
use \Illuminate\Database\Eloquent\Model;
use \Iben\Statable\Statable;
class Post extends Model
{
use Statable;
protected function getGraph()
{
return 'post'; // the SM config to use
}
}
$post = \App\Post::first();
$post->last_state; // returns current state
try {
$post->apply('publish'); // applies transition
} catch (\SM\SMException $e) {
abort(500, $e->getMessage()); // if transition is not allowed, throws exception
}
$post->canApply('publish'); // returns boolean
$post->stateHistory()->get(); // returns PostState collection for the given Post
$post->stateHistory()->where('user_id', \Auth::id())->get(); // you can query history as any Eloquent relation
class Post extends Model
{
// ...
public function getActorId()
{
// return id;
}
}
protected function saveBeforeTransition()
{
return true;
}