-
app/Traits/HasCustomFieldsTrait.php
Open in GitHubuse Crater\Models\CustomField; trait HasCustomFieldsTrait { public function fields() { return $this->morphMany('Crater\Models\CustomFieldValue', 'custom_field_valuable'); } public function addCustomFields($customFields) { foreach ($customFields as $field) { $customField = CustomField::find($field['id']); $customFieldValue = [ 'type' => $customField->type, 'custom_field_id' => $customField->id, 'company_id' => $customField->company_id, getCustomFieldValueKey($customField->type) => $field['value'], ]; $this->fields()->create($customFieldValue); } } // }
-
app/Models/Expense.php
Open in GitHubuse Illuminate\Database\Eloquent\Model; use Crater\Traits\HasCustomFieldsTrait; class Expense extends Model { use HasCustomFieldsTrait; // public static function createExpense($request) { $data = $request->validated(); $data['creator_id'] = Auth::id(); $data['company_id'] = $request->header('company'); $expense = self::create($data); if ($request->hasFile('attachment_receipt')) { $expense->addMediaFromRequest('attachment_receipt')->toMediaCollection('receipts', 'local'); } $customFields = json_decode($request->customFields, true); if ($customFields) { $expense->addCustomFields($customFields); } return $expense; } // }