diff --git a/src/Fieldtypes/Fieldtype.php b/src/Fieldtypes/Fieldtype.php index 1ace92f1..47e0a857 100755 --- a/src/Fieldtypes/Fieldtype.php +++ b/src/Fieldtypes/Fieldtype.php @@ -55,6 +55,11 @@ abstract class Fieldtype */ public $attributes = []; + /** + * @var array + */ + public $traits = null; + /** * @var mixed */ @@ -232,6 +237,16 @@ public function hasRelationship() return !is_null($this->relationship); } + /** + * Determine if the fieldtype has a trait. + * + * @return bool + */ + public function hasTraits() + { + return !is_null($this->traits); + } + /** * Get custom rules when saving field. * @@ -375,6 +390,20 @@ public function getRelationship() return $this->relationship; } + /** + * Get the traits for the fieldtype. + * + * @return string + */ + public function getTraits() + { + if (!$this->traits) { + return null; + } + + return $this->traits; + } + /** * Returns if the fieldtype be eagerloaded. * diff --git a/src/Services/Builders/Builder.php b/src/Services/Builders/Builder.php index fee31afd..cc3d3419 100755 --- a/src/Services/Builders/Builder.php +++ b/src/Services/Builders/Builder.php @@ -29,6 +29,13 @@ abstract class Builder */ protected $relationships = []; + /** + * Traits. + * + * @var array + */ + protected $traits = []; + /** * Generate & return fresh builder instance. * @@ -70,6 +77,7 @@ public function refresh() '{fillable}' => $this->toString($fillable), '{casts}' => $this->toString($casts), '{relationships}' => $this->generateRelationships(), + '{traits}' => $this->generateUseTraitStatements(), ], $this->getPlaceholders()) ) ); @@ -211,6 +219,19 @@ private function generateRelationships() return trim($generated); } + protected function generateUseTraitStatements() + { + $useStatements = []; + + foreach ($this->traits as $traits) { + foreach ($traits as $trait) { + $prefix = Str::startsWith($trait, '\\') ? 'use ' : 'use \\'; + $useStatements[] = $prefix.$trait.';'; + } + } + return implode("\n", $useStatements); + } + /** * Separates column/relationship-based fields. * @@ -222,6 +243,10 @@ private function generateFields() $this->source->fields->each(function ($field) { $fieldtype = $field->type(); + if ($fieldtype->hasTraits()) { + $this->traits[$field->handle] = $fieldtype->getTraits(); + } + if ($fieldtype->hasRelationship()) { $this->relationships[$field->handle] = $field; } else { diff --git a/stubs/builders/collection.stub b/stubs/builders/collection.stub index 4c024d83..9eadb231 100644 --- a/stubs/builders/collection.stub +++ b/stubs/builders/collection.stub @@ -14,6 +14,8 @@ use Illuminate\Database\Eloquent\Builder; class {class} extends Model { use HasOrder, HasActivity; + + {traits} /** * The table associated with the model. diff --git a/stubs/builders/taxonomy.stub b/stubs/builders/taxonomy.stub index cb0f85fd..dedf9fe9 100755 --- a/stubs/builders/taxonomy.stub +++ b/stubs/builders/taxonomy.stub @@ -13,6 +13,8 @@ use Spatie\Activitylog\Models\Activity; class {class} extends Model { use HasOrder, HasActivity; + + {traits} /** * The table associated with the model.