Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling forceCreate() on a MorphMany relation doesn't includes morph type #42796

Closed
DarkGhostHunter opened this issue Jun 13, 2022 · 3 comments
Closed
Labels

Comments

@DarkGhostHunter
Copy link
Contributor

DarkGhostHunter commented Jun 13, 2022

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

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
//       ...
@driesvints driesvints changed the title [9.x] Calling forceCreate() on a MorphMany relation doesn't includes morph type Calling forceCreate() on a MorphMany relation doesn't includes morph type Jun 21, 2022
@driesvints driesvints added the bug label Jun 21, 2022
@AbdlrahmanSaberAbdo
Copy link
Contributor

As I understand make doesn't create a new row in the database it just creates a new instance of the class you request, I tried to test your code with create instead of make, and it's worked well, because create persists to the database, the code will be like this

$post = Post::create(['body' => 'Hello world!']);

$post->comments()->forceCreate(['body' => 'Cool article']);

I'm not sure if it should also work well with make or not because make will not return a new id, so even if he knows the commentable_type, he'll give another error because it didn't know the commentable_id in your case, correct me if I'm wrong?

@DarkGhostHunter
Copy link
Contributor Author

DarkGhostHunter commented Jun 25, 2022

As I understand make doesn't create a new row in the database it just creates a new instance of the class you request, I tried to test your code with create instead of make, and it's worked well, because create persists to the database, the code will be like this

$post = Post::create(['body' => 'Hello world!']);

$post->comments()->forceCreate(['body' => 'Cool article']);

I'm not sure if it should also work well with make or not because make will not return a new id, so even if he knows the commentable_type, he'll give another error because it didn't know the commentable_id in your case, correct me if I'm wrong?

You're correct, my example was bad. I used create, so I fixed the example.

@BrandonSurowiec
Copy link
Contributor

Looks like a fix was accepted: #42929

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants