Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

martin.sinansky's avatar

form to crete multiple pivot records at once?

Hello gents,

I am currently working on a project for a company that creates and sell school books. The editorial office has a section where they manage projects when creating new products. Lately, they came up with a request to create related data in mass. Namely, when they create a new project for a new book, they want to mass assign project partners/suppliers.

The supplier types are authors, lecturers, language correctors, illustrators, printers and such. Since the book can be written by multiple authors, there is a many-to-many relation between the project and supplier models through a pivot table with some additional data (specifically supplier type on the project, his share percentage and some notes).

What I have made in the project so far is a scope bound ProjectSupplier Controller with the standard rest methods. So I can CRUD the project - supplier assignment one by one. I have it routed through Route::resource('project.supplier', ProjectSupplierController::class).

What I have in mind for the mass update is creating a new controller method massAssign() that would route through

 Route::get('/projects/{project}/suppliers/massAssign' ,[ProjectSuppleirController::class,'massAssign'])

, to a view with a form with multiple rows. I have a "simple" structure like this in mind:

form
  formgroup authors
		author1 : id, share, notes
		author2: id, share, notes
  formgroup lecturers
		lecturer1: id, share, notes
 ...

as a result, I expect a formRequest with data in a tree structure like this:

{
'authors' : {
	'author1' : {
		'id': 3,
		'share': 0.7,
		'notes': ' writes the theory...',
		},
	'author2' :  {
		'id':  15,
		'share' : 0.3,
		'notes' : 'writes the excersizes...',
		},
	},
	'lecturers' : {
		'lecturer1': {
			'id': 43,
			'share' : 1,
			'notes' : '',
			},
	},
...
}

I already have some forms, that store multiple related models at once ( like supplier.create view) where I for example name the fields like address[street]. But that referes to a single new Address model that will be creted together with a new Supplier Model.

However, What I am not able to wrap my head around is the correct naming of the inputs to get what I want. Can I do multiple nestings in the form input's name like authors[1][id], authors[1][share] and authors[1][notes]?

I would really appreciate any insight.

0 likes
2 replies
martin.sinansky's avatar

@tykus Sorry bout that. It was late night for me when I posted and completelly forgot I can actually do that...

Please or to participate in this conversation.