You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-12Lines changed: 21 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,34 +49,43 @@ Whenever a `Cat` or `Dog` model is instantiated, the attribute `type` will be se
49
49
50
50
Other than that there is no magic and a `Cat` or a `Dog` model will behave just like a normal Eloquent model. You can define cat or dog specific attributes and relationships on these models. An attribute only set for dogs for example should be a nullable column on the table, so that it is set for dogs but `null` for other pets.
51
51
52
-
### Custom type column
52
+
### Root model behaviour
53
53
54
-
By default the name of the type column is `type`. However, you can set a custom type column name:
54
+
You can query the root model like any other model. However, the returned models will be transformed based by the type name. For example if there is one cat and one dog in the database, `Pet::all();` will return one `Dog` and one `Cat` model.
55
55
56
-
```php
57
-
use ProAI\Inheritance\Inheritance;
56
+
It is not possible to use `new Pet($attributes);` when you specify the type in `$attributes`. Please use the static method `Pet::new($attributes)` instead. This method returns a new model based on the given type. For example if the type in `$attributes` is `App\Models\Cat`, it will return an instance of `App\Models\Cat`. If no type is specified, an instance of `App\Models\Pet` will be returned.
58
57
59
-
class Pet extends Model
58
+
### Custom type names
59
+
60
+
If you want to use different names for the type column than the classname, you can use the static `$inheritanceMap` property:
61
+
62
+
```php
63
+
class Pet
60
64
{
61
65
use Inheritance;
62
66
63
-
protected static $inheritanceColumn = 'pet_type';
67
+
protected static $inheritanceMap = [
68
+
'cat' => Cat::class,
69
+
'dog' => Dog::class,
70
+
];
64
71
}
65
72
```
66
73
67
-
### Custom type name
74
+
### Custom type column
68
75
69
-
If you want a different name than the classname, you can use the `$inheritanceType` property:
76
+
By default the name of the type column is `type`. However, you can set a custom type column name:
70
77
71
78
```php
72
-
class Cat extends Pet
79
+
use ProAI\Inheritance\Inheritance;
80
+
81
+
class Pet extends Model
73
82
{
74
-
protected static $inheritanceType = 'cat';
83
+
use Inheritance;
84
+
85
+
protected static $inheritanceColumn = 'pet_type';
75
86
}
76
87
```
77
88
78
-
> Hint: The value of `$inheritanceType` can also be an enum value.
79
-
80
89
## Support
81
90
82
91
Bugs and feature requests are tracked on [GitHub](https://github.com/proai/eloquent-inheritance/issues).
0 commit comments