Skip to content

Commit c9f633e

Browse files
committed
Add paragraph for root model behaviour
1 parent 8453b8e commit c9f633e

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,43 @@ Whenever a `Cat` or `Dog` model is instantiated, the attribute `type` will be se
4949

5050
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.
5151

52-
### Custom type column
52+
### Root model behaviour
5353

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.
5555

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.
5857

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
6064
{
6165
use Inheritance;
6266

63-
protected static $inheritanceColumn = 'pet_type';
67+
protected static $inheritanceMap = [
68+
'cat' => Cat::class,
69+
'dog' => Dog::class,
70+
];
6471
}
6572
```
6673

67-
### Custom type name
74+
### Custom type column
6875

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:
7077

7178
```php
72-
class Cat extends Pet
79+
use ProAI\Inheritance\Inheritance;
80+
81+
class Pet extends Model
7382
{
74-
protected static $inheritanceType = 'cat';
83+
use Inheritance;
84+
85+
protected static $inheritanceColumn = 'pet_type';
7586
}
7687
```
7788

78-
> Hint: The value of `$inheritanceType` can also be an enum value.
79-
8089
## Support
8190

8291
Bugs and feature requests are tracked on [GitHub](https://github.com/proai/eloquent-inheritance/issues).

0 commit comments

Comments
 (0)