In Laravel PHP Framework, when you have, let's say, a relationship between two tables e.g. one post can have one or multiple comments, you can create the comments of the post these ways:
// Option 1
$post->comments()->create(['text' => 'Greate article...']);
or
// Option 2
Comment::create([
'post_id' => 1,
'text' => 'Greate article...',
]);
Of course, it depends on the cases. Below are my cases.
- For both options, the Post ID 1 has already been validated in the form request whether the post with the ID 1 does exist or not.
- For some reasons, I already need to retrieve the post from the database first, thus I already have the post model.
In these above cases, is the Option 1 more expensive than the Option 2?