S
S
Sasha Ermolaev2020-02-05 13:53:10
Laravel
Sasha Ermolaev, 2020-02-05 13:53:10

How to simultaneously fill two tables from a form?

I want to make simultaneous filling of tables from the form. Tell me how to implement it correctly

Foma

<form class="needs-validation" method="post" novalidate action="reg">
{{ csrf_field() }}
      <h4 class="mb-3">Регистрация клиента</h4>
      <div class="col-md-6 mb-3">
        <label for="fio">ФИО</label>
        <input type="text" class="form-control" name="fio" id="fio" placeholder="" value="" required>
        <div class="invalid-feedback">
          Пожалуйста, введите ФИO.
        </div>
      </div>
      <div class="col-md-6 mb-3">
        <label for="phone">Телефон</label>
        <input type="text" class="form-control" name="phone" id="phone" placeholder="Без пробелов: 89992223355" value="" required>
        <div class="invalid-feedback">
          Пожалуйста, введите номер телефона.
        </div>
      </div>
      <div class="col-md-6 mb-3">
        <label for="gender">Пол</label>
        <select class="custom-select d-block w-100" name="gender" id="gender" value="" required>
          <option>Не выбран</option>
          <option>Мужской</option>
          <option>Женский</option>
        </select>
        <div class="invalid-feedback">
          Пожалуйста, выберите пол.
        </div>
      </div>
      <div class="col-md-6 mb-3">
        <label for="address">Адрес</label>
        <input type="text" class="form-control" name="address" id="address" placeholder="Улица, дом, квартира" value="" required>
        <div class="invalid-feedback">
          Пожалуйста, введите адрес.
        </div>
      </div>
      <h4 class="mb-3">Авто</h4>
      <form class="needs-validation" novalidate>
      <div class="col-md-6 mb-3">
        <label for="Marka">Марка</label>
        <input type="text" class="form-control" name="Marka" id="Marka" placeholder="" value="" required>
          <div class="invalid-feedback">
            Пожалуйста, введите марку авто.
          </div>
      </div>
      <div class="col-md-6 mb-3">
        <label for="Model">Модель</label>
        <input type="text" class="form-control" name="Model" id="Model" placeholder="" value="" required>
        <div class="invalid-feedback">
          Пожалуйста, введите модель авто.
        </div>
      </div>
      <div class="col-md-6 mb-3">
        <label for="Color">Цвет</label>
        <input type="text" class="form-control" name="Color" id="Color" placeholder="" value="" required>
        <div class="invalid-feedback">
          Пожалуйста, введите цвет авто.
        </div>
      </div>
      <div class="col-md-6 mb-3">
        <label for="Number">Гос Номер РФ</label>
        <input type="text" class="form-control" name="Number" id="Number" placeholder='Без пробелов: у588тр34' value="" required>
        <div class="invalid-feedback">
          Пожалуйста, введите гос номер РФ.
        </div>
      </div>
      <hr class="mb-4">
      <div class="custom-control custom-switch">
  <input type="checkbox" class="custom-control-input" id="customSwitch1" name="Status">
  <label class="custom-control-label" for="customSwitch1">Toggle this switch element</label>
</div>
      <hr class="mb-4">
      <button class="btn btn-primary btn-lg btn-block col-md-6 mb-3" type="submit">Зарегистрировать</button>
      </form>

Marches
Route::post('reg', '[email protected]');
Route::post('reg', '[email protected]');

Controller
class regController extends Controller
{
    public function regClient(Request $request){
      DB::table('client')->insert([
          ['fio' => $request->get("fio"),
          'phone' => $request->get("phone"),
          'gender' => $request->get("gender"),
          'address' => $request->get("address")
          ]
      ]);
      return  'Ок';
    }
    public function regAvto(Request $request){
      DB::table('avto')->insert([
          ['Marka' => $request->get("Marka"),
          'Model' => $request->get("Model"),
          'Color' => $request->get("Color"),
          'Number' => $request->get("Number"),
          'Status' => $request->get("Status")
          ]
      ]);
      return  'Ок';
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2020-02-05
@Kostik_1993

In your case, AJAX can be used. But IMHO, as well as generally accepted development principles, it is advised to first register the user, and then his car.
Well, if all the same perversion is your chip
Leave one route and shove everything into one method
Route::post('reg', '[email protected]);

class regController extends Controller
{
    public function regClient(Request $request){

      DB::table('client')->insert([
          ['fio' => $request->get("fio"),
          'phone' => $request->get("phone"),
          'gender' => $request->get("gender"),
          'address' => $request->get("address")
          ]
      ]);
      DB::table('avto')->insert([
          ['Marka' => $request->get("Marka"),
          'Model' => $request->get("Model"),
          'Color' => $request->get("Color"),
          'Number' => $request->get("Number"),
          'Status' => $request->get("Status")
          ]
      ]);
      return  'Ок';
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question