9
9
10
10
abstract class Relation
11
11
{
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
+
12
26
/**
13
27
* The parent model instance
14
28
*
@@ -54,6 +68,7 @@ public function __construct(Model $related, Model $parent)
54
68
{
55
69
$ this ->parent = $ parent ;
56
70
$ this ->related = $ related ;
71
+
57
72
$ this ->query = $ this ->related ::query ();
58
73
59
74
// Build the constraint effect
@@ -62,20 +77,6 @@ public function __construct(Model $related, Model $parent)
62
77
}
63
78
}
64
79
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
-
79
80
/**
80
81
* Get the parent model.
81
82
*
@@ -113,4 +114,31 @@ public function __call(string $method, array $args)
113
114
114
115
return $ result ;
115
116
}
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 ;
116
144
}
0 commit comments