|
4 | 4 |
|
5 | 5 | use Awobaz\Compoships\Compoships; |
6 | 6 | use Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo; |
| 7 | +use Awobaz\Compoships\Database\Eloquent\Relations\BelongsToMany; |
7 | 8 | use Awobaz\Compoships\Database\Eloquent\Relations\HasMany; |
8 | 9 | use Awobaz\Compoships\Database\Eloquent\Relations\HasOne; |
9 | 10 | use Awobaz\Compoships\Exceptions\InvalidUsageException; |
@@ -221,6 +222,89 @@ protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $owne |
221 | 222 | return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation); |
222 | 223 | } |
223 | 224 |
|
| 225 | + /** |
| 226 | + * Define a many-to-many relationship. |
| 227 | + * |
| 228 | + * @template TRelatedModel of \Illuminate\Database\Eloquent\Model |
| 229 | + * |
| 230 | + * @param class-string<TRelatedModel> $related |
| 231 | + * @param string|null $table |
| 232 | + * @param string|array|null $foreignPivotKey |
| 233 | + * @param string|array|null $relatedPivotKey |
| 234 | + * @param string|array|null $parentKey |
| 235 | + * @param string|array|null $relatedKey |
| 236 | + * @param string|null $relation |
| 237 | + * |
| 238 | + * @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, $this> |
| 239 | + */ |
| 240 | + public function belongsToMany( |
| 241 | + $related, |
| 242 | + $table = null, |
| 243 | + $foreignPivotKey = null, |
| 244 | + $relatedPivotKey = null, |
| 245 | + $parentKey = null, |
| 246 | + $relatedKey = null, |
| 247 | + $relation = null |
| 248 | + ) { |
| 249 | + if (is_array($foreignPivotKey)) { |
| 250 | + $this->validateRelatedModel($related); |
| 251 | + } |
| 252 | + |
| 253 | + if (is_null($relation)) { |
| 254 | + $relation = $this->guessBelongsToManyRelation(); |
| 255 | + } |
| 256 | + |
| 257 | + $instance = $this->newRelatedInstance($related); |
| 258 | + |
| 259 | + $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
| 260 | + $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
| 261 | + |
| 262 | + if (is_null($table)) { |
| 263 | + $table = $this->joiningTable($related, $instance); |
| 264 | + } |
| 265 | + |
| 266 | + return $this->newBelongsToMany( |
| 267 | + $instance->newQuery(), |
| 268 | + $this, |
| 269 | + $table, |
| 270 | + $foreignPivotKey, |
| 271 | + $relatedPivotKey, |
| 272 | + $parentKey ?: $this->getKeyName(), |
| 273 | + $relatedKey ?: $instance->getKeyName(), |
| 274 | + $relation |
| 275 | + ); |
| 276 | + } |
| 277 | + |
| 278 | + /** |
| 279 | + * Instantiate a new BelongsToMany relationship. |
| 280 | + * |
| 281 | + * @template TRelatedModel of \Illuminate\Database\Eloquent\Model |
| 282 | + * @template TDeclaringModel of \Illuminate\Database\Eloquent\Model |
| 283 | + * |
| 284 | + * @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query |
| 285 | + * @param TDeclaringModel $parent |
| 286 | + * @param string $table |
| 287 | + * @param string|array $foreignPivotKey |
| 288 | + * @param string|array $relatedPivotKey |
| 289 | + * @param string|array $parentKey |
| 290 | + * @param string|array $relatedKey |
| 291 | + * @param string|null $relationName |
| 292 | + * |
| 293 | + * @return \Awobaz\Compoships\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, TDeclaringModel> |
| 294 | + */ |
| 295 | + protected function newBelongsToMany( |
| 296 | + Builder $query, |
| 297 | + Model $parent, |
| 298 | + $table, |
| 299 | + $foreignPivotKey, |
| 300 | + $relatedPivotKey, |
| 301 | + $parentKey, |
| 302 | + $relatedKey, |
| 303 | + $relationName = null |
| 304 | + ) { |
| 305 | + return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); |
| 306 | + } |
| 307 | + |
224 | 308 | /** |
225 | 309 | * Honor DB::raw instances. |
226 | 310 | * |
|
0 commit comments