Created
November 25, 2014 21:29
-
-
Save graymic/ad4aae39d991b5b6556a to your computer and use it in GitHub Desktop.
Draft system for Eloquent Models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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