Skip to content

Commit 65d0af7

Browse files
committed
HasFields trait
1 parent 986fea8 commit 65d0af7

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ php artisan migrate
6868

6969
### Define the relation with your models
7070

71-
When one of your models has configurable fields, you need to define the relation with the `Fields` model.
71+
When one of your models has configurable fields, you need to add the `HasFields` trait to your model.
7272

7373
```php
7474
use Illuminate\Database\Eloquent\Model;
@@ -77,13 +77,9 @@ use Vormkracht10\Fields\Models\Field;
7777

7878
class Content extends Model
7979
{
80-
// ...
80+
use HasFields;
8181

82-
public function fields(): MorphMany
83-
{
84-
return $this->morphMany(Field::class, 'slug', 'model_type', 'model_key')
85-
->orderBy('position');
86-
}
82+
// ...
8783
}
8884
```
8985

src/Concerns/HasFields.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Vormkracht10\Fields\Concerns;
4+
5+
use Vormkracht10\Fields\Models\Field;
6+
use Illuminate\Database\Eloquent\Relations\MorphMany;
7+
8+
trait HasFields
9+
{
10+
public function fields(): MorphMany
11+
{
12+
return $this->morphMany(Field::class, 'slug', 'model_type', 'model_key')
13+
->orderBy('position');
14+
}
15+
}

0 commit comments

Comments
 (0)