Retrieve the model you are attaching the comment to, then use the save or create method.
For example:
$comment = new App\Comment(['body' => $request->body]);
$post = App\Post::find($post->id);
$comment = $post->comments()->save($comment);
So I have a comments table, Comment model that I am setting up to handle polymorphic relations to multiple models.
Everything is working, I just can’t figure out if there is a cleaner way to create records. Right now I have
'body' => $request->body,
'commentable_id' => $post->id,
'commentable_type' => get_class($post)
]);```
But I am going to have to specify the array attributes anytime I want to create a comment, like 'commentable_type' and 'commentable_id' are never going to be from form data. But I would prefer to not have to specify this in each controller to create a new comment record.
on most models you can do `Post::create($requeset->all())` and it’s cool
I am just wondering if there is a way that I can make it simpler each time I want to insert a new comment (at the code inserted above)?
I was thinking I could override the ‘create’ method on the Comment model, pass the attributes to my method as well as what model it should be attached to, then in my create method, setup the attributes, and call the parent ‘create’ method. But that didn't seem to work, and I think that's because in the above code 'comments()' isn't returning an instance of Comment class, but the Builder class, which I think is correct.
I've searched the docs and searched multiple resources. Can't quite find this one.
Any thoughts? Thanks in advance!
Retrieve the model you are attaching the comment to, then use the save or create method.
For example:
$comment = new App\Comment(['body' => $request->body]);
$post = App\Post::find($post->id);
$comment = $post->comments()->save($comment);
Please or to participate in this conversation.