lundi 16 septembre 2019

How to insert data with multiple foreign keys in Laravel 5.7 with Eloquente?

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
                                                              value

I created the models with reliese package and the columns of the "Contract_types" and "Fields_contract" were filled with Seeders, with that I have the ID of the different Contract Types. Now each field of the form have a unique name for each field and thats my problem, when I try to join the request with the values with "fields_contract_id and "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>
              <div class="col-3">
                <input type="text" class="form-control" placeholder="Date / Day pf the Week" name="date" value=>
                {!! $errors->first('date','<span class="form-text" >:message</span>') !!}
              </div>
              <div class="col-1">
                <p>of</p>
              </div>
              <div class="col-2">
                <input type="text" class="form-control" placeholder="Month" name="month" value=>
                {!! $errors->first('month','<span class="form-text" >:message</span>') !!}
              </div>
              <div class="col-4">
                <p>of  2019, beetween the company</p>
              </div>
            </div>
            <div class="row mb-4">
              <div class="col-12">
                <input type="text" class="form-control" placeholder="Company Name" name="company_name" value=>
                {!! $errors->first('company_name','<span class="form-text" >:message</span>') !!}
              </div>
</div>

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

Regards



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire