Skip to content

Instantly share code, notes, and snippets.

@graymic
Created November 25, 2014 21:29
Show Gist options
  • Save graymic/ad4aae39d991b5b6556a to your computer and use it in GitHub Desktop.
Save graymic/ad4aae39d991b5b6556a to your computer and use it in GitHub Desktop.
Draft system for Eloquent Models
<?php namespace path\to\your\traits\directory;
/**
* Class DraftTrait
*
* @package app\acme\Services\Traits
*/
trait DraftTrait {
/**
* Trait override for draft features.
*
* @param array $attributes
*/
public function save(Array $attributes = [])
{
// Upon saving the model event...
parent::saving(function ($model) {
// If we are previewing the model change.
if (\Input::get('preview')) {
// Set the model into the session.
\Session::set(__CLASS__ . '.' . $model->id, $this);
// Don't save to the model.
return false;
}
// Clear the session if we've decided to publish.
\Session::forget(__CLASS__ . '.' . $this->id);
});
// Continue saving as usual...
parent::save($attributes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment