This is the code I'm trying to work with, and I get 'no-image.jpg' in my database field and as I add the second dynamic row in my form when I submit it I get 'Undefined offset: 1'. Everything works fine except file input.
This is part of my form which is dynamic and I can add more rows:
<table id="myTable" class=" table order-list">
<thead>
<tr>
<td>{{Form::label('Payment', 'پرداخت')}}</td>
<td>{{Form::label('PaymentDate', 'تاریخ')}}</td>
<td>{{Form::label('Account', 'حساب')}}</td>
<td>{{Form::label('Attachment_link', 'تصویر پرداخت')}}</td>
</tr>
</thead>
<tbody>
<tr>
<td class="col-md-2">
{{Form::text('pay[]', '', ['class' => 'form-control number', 'placeholder' => ''])}}
</td>
<td class="col-md-2">
{{Form::text('payDate[]', '', ['class' => 'form-control pDate', 'placeholder' => ''])}}
</td>
<td class="col-md-2">
<select name="account[]" class="form-control selectpicker">
<option></option>
<option>دفتر وکالت</option>
<option>دفتر موسسه</option>
</select>
</td>
<td class="col-md-1">
{{Form::file('attachment_link[]')}}
</td>
<td class="col-md-2"><a class="deleteRow"></a>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: left;">
<input type="button" class="btn btn-lg btn-block btn-primary" id="addrow" value="اضافه کردن پرداخت جدید" />
</td>
</tr>
<tr>
</tr>
</tfoot>
</table>
This is my controller:
$input = $request->all();
$pays = $request->input('pay');
$paymentDates = $request->input('payDate');
$account = $request->input('account');
$attachment = $request->file('attachment_link');
for($i=0; $i< count($input['pay']); $i++) {
if ($request->hasFile($attachment[$i])) {
$fileNameWithExt = $request->file($attachment[$i])->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $request->file($attachment[$i])->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . '_' . time() . '_' . $extention;
// Upload Image
$path = $request->file('attachment_link')->storeAs('public/content-images/products/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$payment = new Payment;
$payment->file = $file;
$prePayment = $pays[$i];
$payment->payment = str_replace(',','',$prePayment);
$payment->paymentDate = $paymentDates[$i];
$payment->account = $account[$i];
$payment->attachment_link = $fileNameToStore;
$payment->save();
}
$request->hasFile('attachment_link')