@@ -120,17 +120,30 @@ public function deleting(Asset $asset)
120120 $ logAction ->user_id = Auth::id ();
121121 $ logAction ->logaction ('delete ' );
122122 }
123-
123+
124+ /**
125+ * Executes every time an asset is saved.
126+ *
127+ * This matters specifically because any database fields affected here MUST already exist on
128+ * the assets table (and/or any related models), or related migrations WILL fail.
129+ *
130+ * For example, if there is a database migration that's a bit older and modifies an asset, if the save
131+ * fires before a field gets created in a later migration and that field in the later migration
132+ * is used in this observer, it doesn't actually exist yet and the migration will break unless we
133+ * use saveQuietly() in the migration which skips this observer.
134+ *
135+ * @see https://github.com/snipe/snipe-it/issues/13723#issuecomment-1761315938
136+ */
124137 public function saving (Asset $ asset )
125138 {
126- //determine if calculated eol and then calculate it - this should only happen on a new asset
127- if (is_null ($ asset ->asset_eol_date ) && !is_null ($ asset ->purchase_date ) && !is_null ($ asset ->model ->eol )){
139+ // determine if calculated eol and then calculate it - this should only happen on a new asset
140+ if (is_null ($ asset ->asset_eol_date ) && !is_null ($ asset ->purchase_date ) && !is_null ($ asset ->model ->eol )){
128141 $ asset ->asset_eol_date = $ asset ->purchase_date ->addMonths ($ asset ->model ->eol )->format ('Y-m-d ' );
129142 $ asset ->eol_explicit = false ;
130143 }
131144
132- //determine if explicit and set eol_explit to true
133- if (!is_null ($ asset ->asset_eol_date ) && !is_null ($ asset ->purchase_date )) {
145+ // determine if explicit and set eol_explicit to true
146+ if (!is_null ($ asset ->asset_eol_date ) && !is_null ($ asset ->purchase_date )) {
134147 if ($ asset ->model ->eol ) {
135148 $ months = Carbon::parse ($ asset ->asset_eol_date )->diffInMonths ($ asset ->purchase_date );
136149 if ($ months != $ asset ->model ->eol ) {
0 commit comments