PHP Laravel how to use two forms in one view

840

I'm building aLaravel-app where I have two forms in oneblade-template, which appears depending on which tab is active.

It looks like this:

<div data-contact-form-id="1" id="contact-company">
  <form method="POST" action="{{ route('contact.company') }}">
  // bunch of input fields here
  </form>
</div>
<div data-contact-form-id="2" id="contact-private">
  <form method="POST" action="{{ route('contact.store') }}">
  // bunch of input fields here
  </form>
</div>

then myweb.php

Route::post('contact', '[email protected]')->name('contact.store');
Route::post('contact/company', '[email protected]')->name('contact.company');

but I can't submit the "company-contact" form, and when I try to do remove the slash in the route I get an errorroute.store is not defined:

Route::post('contact', '[email protected]')->name('contact.store');
Route::post('contact', '[email protected]')->name('contact.company');

Why is this and how can I solve this?

433

Answer

Solution:

put all input fields in a single form and submit to the store route.

In controller:

Here I used the company model & some fields just for example you can replace with your model & your field name

public function store(Request $request){
  $post = $request->all();

  Company::create([
      'company_name' => $post['company_name']
      'company_email' => $post['company_email'],
      'company_phone' => $post['company_phone']
  ]);

   // another model for contact
   Contact::create([
     'name' => $post['name'],
     'email' => $post['email'],
     'phone' => $post['phone'],
   ]);
}

People are also looking for solutions to the problem: How to parse PHP Doctrine Querybuilder DateTime from a HTTPResponseMessage to C# DateTime?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.