Repository: laravel/framework (php)

Issue:
Calling `forceCreate()` on a MorphMany relation doesn't includes morph type
<!-- DO NOT THROW THIS AWAY -->
<!-- Fill out the FULL versions with patch versions -->

- Laravel Version: 9.17
- PHP Version: 8.0.23
- Database Driver & Version: N/A

### Description:

When using `forceCreate()` on a morph relation (`morphOne|morphMany`), the morph type column is not added.

```
$post->comments()->forceCreate(['body' => 'Cool article']);
```

This behavior is realated to #42281.

### Steps To Reproduce:

```php
class Post extends Model
{
    public $fillable = ['*'];
    public function comments() { return $this->morphMany(Comment::class, 'commentable'); }
}

class Comment extends Model
{
    public function commentable() { return $this->morphTo(); }
}

$post = Post::create(['body' => 'Hello world!']);

$post->comments()->forceCreate(['body' => 'Cool article']);

// Illuminate\Database\QueryException : 
//   SQLSTATE[23000]: 
//     Integrity constraint violation: 
//       19 NOT NULL constraint failed: comments.commentable_type
//       ...
```

Pull request description:
Fixed bug on forceCreate on a MorphMay relationship  not including morph type
Fixes #42796

`forceCreate` was not including the morph type on the attributes sent to create the related model. The  forceCreate method has been overrided to include the morph type on the attributes before passing the call to its parent.

This should solve the #42796 issue.


Patch:
diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php
index 12b0650263..6869e5bacd 100755
--- a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php
+++ b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php
@@ -46,4 +46,16 @@ public function match(array $models, Collection $results, $relation)
     {
         return $this->matchMany($models, $results, $relation);
     }
+
+    /**
+     * Create a new instance of the related model. Allow mass-assignment.
+     *
+     * @param  array  $attributes
+     * @return \Illuminate\Database\Eloquent\Model
+     */
+    public function forceCreate(array $attributes = []) 
+    {
+        $attributes[$this->getMorphType()] = $this->morphClass;
+        parent::forceCreate($attributes);
+    }
 }


Test patch:
diff --git a/tests/Database/DatabaseEloquentMorphOneOfManyTest.php b/tests/Database/DatabaseEloquentMorphOneOfManyTest.php
index 4d98d120ef..9a8e70f4c7 100644
--- a/tests/Database/DatabaseEloquentMorphOneOfManyTest.php
+++ b/tests/Database/DatabaseEloquentMorphOneOfManyTest.php
@@ -94,6 +94,15 @@ public function testMorphType()
         $this->assertSame('active', $product->current_state->state);
     }
 
+    public function testForceCreateMorphType()
+    {
+        $product = MorphOneOfManyTestProduct::create();
+        $product->states()->forceCreate([
+            'state' => 'active',
+        ]);
+        $this->assertSame(MorphOneOfManyTestProduct::class, $product->current_state->stateful_type);
+    }
+
     public function testExists()
     {
         $product = MorphOneOfManyTestProduct::create();