Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Fieldtypes/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ abstract class Fieldtype
*/
public $attributes = [];

/**
* @var array
*/
public $traits = null;

/**
* @var mixed
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Services/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ abstract class Builder
*/
protected $relationships = [];

/**
* Traits.
*
* @var array
*/
protected $traits = [];

/**
* Generate & return fresh builder instance.
*
Expand Down Expand Up @@ -70,6 +77,7 @@ public function refresh()
'{fillable}' => $this->toString($fillable),
'{casts}' => $this->toString($casts),
'{relationships}' => $this->generateRelationships(),
'{traits}' => $this->generateUseTraitStatements(),
], $this->getPlaceholders())
)
);
Expand Down Expand Up @@ -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.
*
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions stubs/builders/collection.stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use Illuminate\Database\Eloquent\Builder;
class {class} extends Model
{
use HasOrder, HasActivity;

{traits}

/**
* The table associated with the model.
Expand Down
2 changes: 2 additions & 0 deletions stubs/builders/taxonomy.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use Spatie\Activitylog\Models\Activity;
class {class} extends Model
{
use HasOrder, HasActivity;

{traits}

/**
* The table associated with the model.
Expand Down