Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to add attachment to dynamic form input field

I am trying to implement dynamic form input field in Laravel. Everything has been successful until when I got to the point of adding attachment. I don't know how to go about this in my Controller and view.

Model

class Invoice extends Model
{
    public function sale(){
      return $this->hasMany('App\Sales');
  }

    public function customer(){
      return $this->belongsTo('App\Customer');
  }
}


class Sale extends Model
{
    public function invoice(){
      return $this->belongsTo('App\Invoice');
    }

    public function product(){
      return $this->belongsTo('App\Product');
    }
}

Controller

public function create()
{
    $customers = Customer::all();
    $products = Product::all();
    return view('invoice.create', compact('customers','products'));
}

public function store(Request $request)
{
    $request->validate([
        'customer_id' => 'required',
        'product_id' => 'required',
        'qty' => 'required',
        'price' => 'required',
        'dis' => 'required',
        'amount' => 'required',
    'document' => 'nullable|mimes:doc,docx,pdf|max:2000',
    ]);

    $invoice = new Invoice();
    $invoice->customer_id = $request->customer_id;
    $invoice->total = 1000;
    $invoice->save();

    if ($request->document != "") {
        $document = time() . '_' . $request->document_path->getClientOriginalName();
        $request->document->move('storage/documents/', $document);
        $arr['url'] = 'storage/documents/' . $document;
    }

    foreach ( $request->product_id as $key => $product_id){
        $sale = new Sale();
        $sale->qty = $request->qty[$key];
        $sale->price = $request->price[$key];
        $sale->dis = $request->dis[$key];
        $sale->amount = $request->amount[$key];
        $sale->product_id = $request->product_id[$key];
        $sale->document_path = $request->document[$key];
        $sale->invoice_id = $invoice->id;
        $sale->save();
     }

     return redirect('invoice/'.$invoice->id)->with('message','invoice created Successfully');
}


public function edit($id)
{
    $customers = Customer::all();
    $products = Product::orderBy('id', 'DESC')->get();
    $invoice = Invoice::findOrFail($id);
    $sales = Sale::where('invoice_id', $id)->get();
    return view('invoice.edit', compact('customers','products','invoice','sales'));
}

public function update(Request $request, $id)
{
    $request->validate([

    'customer_id' => 'required',
    'product_id' => 'required',
    'qty' => 'required',
    'price' => 'required',
    'dis' => 'required',
    'amount' => 'required',
]);

    $invoice = Invoice::findOrFail($id);
    $invoice->customer_id = $request->customer_id;
    $invoice->total = 1000;
    $invoice->save();

    if ($request->document != "") {
        $document = time() . '_' . $request->document_path->getClientOriginalName();
        $request->document->move('storage/documents/', $document);
        $arr['url'] = 'storage/documents/' . $document;
    }

    Sale::where('invoice_id', $id)->delete();

    foreach ( $request->product_id as $key => $product_id){
        $sale = new Sale();
        $sale->qty = $request->qty[$key];
        $sale->price = $request->price[$key];
        $sale->dis = $request->dis[$key];
        $sale->amount = $request->amount[$key];
        $sale->product_id = $request->product_id[$key];
        $sale->document = $request->document[$key];
        $sale->invoice_id = $invoice->id;
        $sale->save();
    }

     return redirect('invoice/'.$invoice->id)->with('message','invoice created Successfully');
}

create.blade.view

     <div class="row">
         <div class="clearix"></div>
        <div class="col-md-12">
            <div class="tile">
                <h3 class="tile-title">Invoice</h3>
                <div class="tile-body">
                    <form  method="POST" action="{{route('invoice.store')}}">
                        @csrf
                        <div class="form-group col-md-3">
                            <label class="control-label">Customer Name</label>
                            <select name="customer_id" class="form-control">
                                <option>Select Customer</option>
                                @foreach($customers as $customer)
                                    <option name="customer_id" value="{{$customer->id}}">{{$customer->name}} </option>
                                @endforeach
                            </select>                            </div>
                        <div class="form-group col-md-3">
                            <label class="control-label">Date</label>
                            <input name="date"  class="form-control datepicker"  value="<?php echo date('Y-m-d')?>" type="date" placeholder="Enter your email">
                        </div>



                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th scope="col">Product Name</th>
                            <th scope="col">Qty</th>
                            <th scope="col">Price</th>
                            <th scope="col">Discount</th>
                            <th scope="col">Amount</th>
                            <th scope="col"><a class="addRow"><i class="fa fa-plus"></i></a></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td><select name="product_id[]" class="form-control productname" >
                                    <option>Select Product</option>
                                @foreach($products as $product)
                                        <option name="product_id[]" value="{{$product->id}}">{{$product->name}}</option>
                                    @endforeach
                                </select></td>
                            <td><input type="text" name="qty[]" class="form-control qty" ></td>
                            <td><input type="text" name="price[]" class="form-control price" ></td>
                            <td><input type="text" name="dis[]" class="form-control dis" ></td>
                            <td><input type="text" name="amount[]" class="form-control amount" ></td>
                            <td><input type="file" class="form-control" name="document[]" multiple></td>
                            <td><a   class="btn btn-danger remove"> <i class="fa fa-remove"></i></a></td>
                         </tr>
                        </tbody>

                    </table>

                        <div >
                            <button class="btn btn-primary" type="submit">Submit</button>
                        </div>
                 </form>
                </div>
            </div>


            </div>
        </div>

</main>

@endsection
@push('js')
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
   <script src="{{asset('/')}}js/multifield/jquery.multifield.min.js"></script>

   <script type="text/javascript">
     $(document).ready(function(){

        $('.addRow').on('click', function () {
            addRow();

        });

        function addRow() {
            var addRow = '<tr>\n' +
                '         <td><select name="product_id[]" class="form-control productname " >\n' +
                '         <option value="0" selected="true" disabled="true">Select Product</option>\n' +
'                                        @foreach($products as $product)\n' +
'                                            <option value="{{$product->id}}">{{$product->name}}</option>\n' +
'                                        @endforeach\n' +
                '               </select></td>\n' +
'                                <td><input type="text" name="qty[]" class="form-control qty" ></td>\n' +
'                                <td><input type="text" name="price[]" class="form-control price" ></td>\n' +
'                                <td><input type="text" name="dis[]" class="form-control dis" ></td>\n' +
'                                <td><input type="text" name="amount[]" class="form-control amount" ></td>\n' +
'                                <td><input type="file" name="document[]" class="form-control document" ></td>\n' +
'                                <td><a   class="btn btn-danger remove"> <i class="fa fa-remove"></i></a></td>\n' +
'                             </tr>';
            $('tbody').append(addRow);
        };

        $('.remove').live('click', function () {
            var l =$('tbody tr').length;
            if(l==1){
                alert('you cant delete last one')
            }else{

                $(this).parent().parent().remove();

            }

        });
    });

   </script>

@endpush

I started the code, but don't know how to complete it. Invoice is the main controller. The attachment is in Sale.

From my code, How do I add attachment to my dynamic form input array in the Controller and view and save it.

Thank you

0 likes
0 replies

Please or to participate in this conversation.