99
1010abstract class Relation
1111{
12+ /**
13+ * The foreign key of the parent model.
14+ *
15+ * @var string
16+ */
17+ protected string $ foreign_key ;
18+
19+ /**
20+ * The associated key on the parent model.
21+ *
22+ * @var string
23+ */
24+ protected string $ local_key ;
25+
1226 /**
1327 * The parent model instance
1428 *
@@ -54,6 +68,7 @@ public function __construct(Model $related, Model $parent)
5468 {
5569 $ this ->parent = $ parent ;
5670 $ this ->related = $ related ;
71+
5772 $ this ->query = $ this ->related ::query ();
5873
5974 // Build the constraint effect
@@ -62,20 +77,6 @@ public function __construct(Model $related, Model $parent)
6277 }
6378 }
6479
65- /**
66- * Set the base constraints on the relation query.
67- *
68- * @return void
69- */
70- abstract public function addConstraints (): void ;
71-
72- /**
73- * Get the results of the relationship.
74- *
75- * @return mixed
76- */
77- abstract public function getResults (): mixed ;
78-
7980 /**
8081 * Get the parent model.
8182 *
@@ -113,4 +114,31 @@ public function __call(string $method, array $args)
113114
114115 return $ result ;
115116 }
117+
118+ /**
119+ * Create a new row of the related
120+ *
121+ * @param array $attributes
122+ * @return Model
123+ */
124+ public function create (array $ attributes ): Model
125+ {
126+ $ attributes [$ this ->foreign_key ] = $ this ->parent ->getKeyValue ();
127+
128+ return $ this ->related ->create ($ attributes );
129+ }
130+
131+ /**
132+ * Get the results of the relationship.
133+ *
134+ * @return mixed
135+ */
136+ abstract public function getResults (): mixed ;
137+
138+ /**
139+ * Set the base constraints on the relation query.
140+ *
141+ * @return void
142+ */
143+ abstract public function addConstraints (): void ;
116144}
0 commit comments