1

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();
    }
5
  • which line you are getting the error?? Commented Jan 15, 2018 at 9:24
  • Can you give snippet code for your form? Commented Jan 15, 2018 at 9:25
  • @DharmaSaputra I added my form snippet too. Thank you Commented Jan 15, 2018 at 9:36
  • Hey @Sohel0415 this is the code you helped me with yesterday, If I don't add dynamic input in my form I don't get an error and also file doesn't upload to folder and I get no-image.jpg in my database. when I add dynamic input in my form same happens and I get error in this line:if ($request->hasFile($attachment[$i])) { – Commented Jan 15, 2018 at 9:37
  • $request->hasFile('attachment_link') Commented Jan 15, 2018 at 9:41

1 Answer 1

1

Try this-

if (isset($attachment[$i])&&is_file($attachment[$i])) {
            $fileNameWithExt = $attachment[$i]->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extention = $attachment[$i]->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore = $filename . '_' . time() . '_' . $extention;
            // Upload Image
            $path = $attachment[$i]->storeAs('public/content-images/products/', $fileNameToStore);
        } else {
            $fileNameToStore = 'no-image.jpg';
        }
Sign up to request clarification or add additional context in comments.

7 Comments

Perfect, it works fine but when I add the second row to my form I get 'Undefined offset: 1' in this line : if (is_file($attachment[$i])) {
it works without error but second image in database is 'no-image.jpg' and it didn't uploaded in the folder too.
you need to put a image with the same name and extension of no-image.jpg manually. Its indicate a string of a image name, not the actual image file
I mean when I fill the form and I have two row , two different images, only one image uploads and goes to database, second one doesnt go to the folder and database.
seems pretty odd, try to debug your code step by step, it should have worked though. Did you try changing full code?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.