Hamid Ghiasifar
Hamid Ghiasifar

Reputation: 47

Laravel create one to many relation in single form

Hi I want to add company and directors of that in one page(create.company.blade.php).

in company create page I fill inputs of company information and after that a part exist for directors.

what's the best way to doing that? I can crud directors with ajax but how I can attaching these entities together when submit company create? tow section using one form.

Upvotes: 0

Views: 94

Answers (1)

James Clark
James Clark

Reputation: 1325

Submitting the directors as a separate post request via ajax is the cleanest option. This allows you to keep the directors and company storing processes separate.

In order to make this work, you will need to send the company_id along with the director information in your ajax request.

The implementation details of how you do this will depend on your javascript environment and how the page is working. If you are working in a modern javascript framework such as angular, react, or vue.js you can accomplish this by having your company controller return the record it has created, and storing that into a variable within javascript when the ajax call completes. The company.id can then be appended to the data you send in the body of your director ajax post request.

If, instead of a modern javascript framework, you are using jquery, you might need to add a hidden company_id field to your director form and populate it's value after the company ajax call instead.

Upvotes: 1

Related Questions