J
J
jazzus2018-12-25 10:27:31
Laravel
jazzus, 2018-12-25 10:27:31

How to create an entry in a Laravel join table without accessing it?

There are 3 models:
Product
Comment
ProductComment
Is it possible to create an entry in a ProductComment without referring to it? To avoid having to do
$productcomment = new ProductComment;
$productcomment->save();
Many-to-many relationship, comments may not be the best example, but still.
I just don't like the idea of ​​referring to junction tables, and Laravel probably has a way of not doing that and writing something like $product->comment-create (that's Laravel, after all)).
update
while writing the question, the idea arose to write in Product createComment () with an entry in the linking table, but perhaps there is a purely Laravelian way

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2018-12-25
@jazzus

Bottom line:
Laravel does have ways of not having to access intermediate tables.
If there is no record (for example, comments), then the option proposed by Koteezy
will do. In my case, the records already existed and they only needed to be connected, so the attach option is suitable
doc on the topic
https://laravel.ru/docs/v5/eloquent-relationships

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question