@@ 39-61 (lines=23) @@ | ||
36 | * |
|
37 | * @return bool |
|
38 | */ |
|
39 | public function publish(array $options = []) |
|
40 | { |
|
41 | // https://laravel-news.com/laravel-model-events-getting-started |
|
42 | // https://gist.github.com/scrubmx/7fc20663ce2b3ac103a2879915b572be |
|
43 | // https://www.google.com/search?q=laravel+register+fire+model+event&oq=laravel+register+fire+model+event |
|
44 | ||
45 | // If the "publishing" event returns false we bail out immediately and return false, |
|
46 | // indicating that the save failed. This provides a chance for any |
|
47 | // listeners to cancel save operations if validations fail or whatever. |
|
48 | if ($this->fireModelEvent('publishing') === false) { |
|
49 | return false; |
|
50 | } |
|
51 | ||
52 | $this->{$this->getPublishedAtColumn()} = $this->freshTimestamp(); |
|
53 | ||
54 | $saved = parent::save($options); |
|
55 | ||
56 | if ($saved) { |
|
57 | $this->fireModelEvent('published', false); |
|
58 | } |
|
59 | ||
60 | return $saved; |
|
61 | } |
|
62 | ||
63 | /** |
|
64 | * Toogle model instance state to non-published. |
|
@@ 70-85 (lines=16) @@ | ||
67 | * |
|
68 | * @return bool |
|
69 | */ |
|
70 | public function unpublish(array $options = []) |
|
71 | { |
|
72 | if ($this->fireModelEvent('unpublishing') === false) { |
|
73 | return false; |
|
74 | } |
|
75 | ||
76 | $this->{$this->getPublishedAtColumn()} = null; |
|
77 | ||
78 | $saved = parent::save($options); |
|
79 | ||
80 | if ($saved) { |
|
81 | $this->fireModelEvent('unpublished', false); |
|
82 | } |
|
83 | ||
84 | return $saved; |
|
85 | } |
|
86 | ||
87 | /** |
|
88 | * Save instance of this model as a draft. |