mirror of
				https://github.com/crater-invoice/crater.git
				synced 2025-10-30 21:21:09 -04:00 
			
		
		
		
	v5.0.0 update
This commit is contained in:
		| @ -11,9 +11,21 @@ trait HasCustomFieldsTrait | ||||
|         return $this->morphMany('Crater\Models\CustomFieldValue', 'custom_field_valuable'); | ||||
|     } | ||||
|  | ||||
|     protected static function booted() | ||||
|     { | ||||
|         static::deleting(function ($data) { | ||||
|             if ($data->fields()->exists()) { | ||||
|                 $data->fields()->delete(); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     public function addCustomFields($customFields) | ||||
|     { | ||||
|         foreach ($customFields as $field) { | ||||
|             if (! is_array($field)) { | ||||
|                 $field = (array)$field; | ||||
|             } | ||||
|             $customField = CustomField::find($field['id']); | ||||
|  | ||||
|             $customFieldValue = [ | ||||
| @ -30,6 +42,10 @@ trait HasCustomFieldsTrait | ||||
|     public function updateCustomFields($customFields) | ||||
|     { | ||||
|         foreach ($customFields as $field) { | ||||
|             if (! is_array($field)) { | ||||
|                 $field = (array)$field; | ||||
|             } | ||||
|  | ||||
|             $customField = CustomField::find($field['id']); | ||||
|             $customFieldValue = $this->fields()->firstOrCreate([ | ||||
|                 'custom_field_id' => $customField->id, | ||||
| @ -45,8 +61,21 @@ trait HasCustomFieldsTrait | ||||
|  | ||||
|     public function getCustomFieldBySlug($slug) | ||||
|     { | ||||
|         return $this->fields()->with('customField')->whereHas('customField', function ($query) use ($slug) { | ||||
|             $query->where('slug', $slug); | ||||
|         })->first(); | ||||
|         return $this->fields() | ||||
|             ->with('customField') | ||||
|             ->whereHas('customField', function ($query) use ($slug) { | ||||
|                 $query->where('slug', $slug); | ||||
|             })->first(); | ||||
|     } | ||||
|  | ||||
|     public function getCustomFieldValueBySlug($slug) | ||||
|     { | ||||
|         $value = $this->getCustomFieldBySlug($slug); | ||||
|  | ||||
|         if ($value) { | ||||
|             return $value->defaultAnswer; | ||||
|         } | ||||
|  | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user