8 months ago

How to insert data with multiple foreign keys ?

Posted 8 months ago by reneko18

I'm working in a web application with Laravel 5.7, where I create differents employment contracts each one is a blade view with a form with input text between the content of the contract. So when a user fill and submit the contract, the user can fill another form with name and email to receive the link of the contract via email, and with it open a dynamic PDF with the fields that filled. My problem is the relation between foreign keys, specifically with the values that a user fill in the form and a contract.

The structure of my database is:

User   Contract_types  Fields_contracts  Contracts            Values
----   --------------  ---------------  ---------           --------
id           id            id               id                 id
name         name     contract_types_id  contract_types_id  fields_contract_id
email    description     field_name       user_id          contract_id
$contract = Contract::create([
       'contract_types_id' => request('id'),
    $contractid = $contract->id; 
// With it I have the contract_id value for column "Values"

$idfields = FieldContract::where('contract_types_id', 1)->pluck('id'); // With it I have the IDs of each field name that belongs to the contract_types ID 1

Part of the form:

        <div class="row mb-4">
          <div  class="col-2">
            <p>In Valparaíso</p>
          <div class="col-3">
            <input type="text" class="form-control" placeholder="Date / Day pf the Week" name="date" value={{ old('date') }}>
            {!! $errors->first('date','<span class="form-text" >:message</span>') !!}
          <div class="col-1">
          <div class="col-2">
            <input type="text" class="form-control" placeholder="Month" name="month" value={{ old('month') }}>
            {!! $errors->first('month','<span class="form-text" >:message</span>') !!}
          <div class="col-4">
            <p>of  2019, beetween the company</p>
        <div class="row mb-4">
          <div class="col-12">
            <input type="text" class="form-control" placeholder="Company Name" name="company_name" value={{ old('company_name') }}>
            {!! $errors->first('company_name','<span class="form-text" >:message</span>') !!}

I don't know how to make the relation to insert all the data in "Values" column, I appreciate some orientation to resolve my problem


Please sign in or create an account to participate in this conversation.