0

i try to build form to create invoice and then generate it pdf

My Db structure

tables:

-- invoices contain date | invoice_num | client_id

-- articles contain name | description | price

-- article_invoice contain invoice_id | article_id | qte*(Quantity)*

Using a BelongsToMany Relation to save article_id & invoice_id

My Invoice Controller

  public function store(Request $request){
      $qte = $request['qte'];
  
      $request = $request['fields'];

      $facture = Facture::create(['date'=>'2018-08-22', 'facture_num'=>'4','client_id'=>'6']);

      $facture->article()->sync($request);

      $facture->article()->update($qte);

  }

my view

<div class="controls">
    {!! Form::open(array('route' =>'factures.store' ,'method'=>'post','enctype'=>'multipart/form-data')) !!}
    <div class="entry input-group col-md-6">
        <select class="form-control" name="fields[]" type="text" placeholder="Type something">
            @foreach($articles as $article)
                <option value="{{$article->id}}">{{ $article->designation}}</option>
            @endforeach
        </select>
        <input type="number" name="qte[]">

        <span class="input-group-btn">
            <button class="btn btn-success btn-add" type="button">
                <span class="glyphicon glyphicon-plus"></span>
            </button>
        </span>
    </div>
    <br>
</div>
</div>

<button type="submit" class="btn btn-primary btn-sm">
        <i class="fa fa-dot-circle-o"></i> Submit
</button>
{!! Form::close() !!}

**after syncronize article_invoice i want to save the qte any help please ? **

Community
  • 1
  • 1
Ala Chebil
  • 451
  • 1
  • 8
  • 19

2 Answers2

0

you can pass your extra columns like -> $facture->article()->sync([$request,$extra_columns]);

Usman Jdn
  • 807
  • 8
  • 21
  • `SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `article_facture` (`article_id`, `facture_id`, `0`, `1`) values (0, 8, 7, 8))` – Ala Chebil Jul 23 '18 at 13:25
  • write the name of the column you want to sync https://stackoverflow.com/a/27230803/8830631 – Usman Jdn Jul 24 '18 at 08:47
0

Solution:

Controller :

 $data = Input::get('qte'); // get input qte ( extra column )
 $fields = Input::get('fields'); // get input ( Ids Of articles )

 $syncData = array();

  for ($i=0;$i<count($fields);$i++){

      $syncData[$fields[$i]] = array('qte' => $data[$i]);

  }

  $facture->article()->sync($syncData);

$syncData must be like this :

array:3 [▼   
         7 => array:1 [▼                    // " 7 " is article Id
                       "qte" => "4"   ]    //qte is the extra column.
         8 => array:1 [▼
                       "qte" => "10"   ] 
         10 => array:1 [▼
                        "qte" => "250"   ] ]
Ala Chebil
  • 451
  • 1
  • 8
  • 19