I create the following database structure to manage different service contents for different batch and just because this structure must be replicated for sub services too I decided to use a "Many To Many Polymorphic Relations" with Laravel.
To create a new record in table "service_and_relatives_contents" and another association record in table "service____relative_content_batch_a" (my pivot table) I used :
$service -> relatives() -> create($arrRelatives)
the problem is with that code is that I can't populate "batches_id" fields in my pivot table. For this reason I used the following code :
$relativeId = $service->relatives()->create($arrRelatives)->id;
$service->relatives()->updateExistingPivot($relativeId,['batches_id'=>$keyRelatives]);
It works well but it is the right way to create and associate a record in the same time?
Please consider this as an example, I will use laravel conventions for table name and foreign-key for the live version.
.