konrms
4 months ago
57
11
Laravel

Help with csv file uploading...

Posted 4 months ago by konrms

Hello guys. I'm trying to upload a .csv file to my (oracle) database. In fact the end user should insert this file along with other data. After submitting, a pdf file should be created.

I have created class CreateCsvDataTable (php migrate) with schema csv_data and the table csv_data exists in my oracle. It should be populated without headers.

My trouble is with csv uploading (please have a look at point (3)). When giving dd($header); I get this error:

Call to a member function getRealPath() on null

Could you please assist?

(1) My web.php routes file is the following:

Route::get('/diploma', '[email protected]');
Route::post('/hard', '[email protected]');

Route::get('/p', '[email protected]');
Route::get('/export1','[email protected]')->name('export1.pdf');

(2) The form code (blade excerpt) with data requested is this:

    <form method='post' action="/hard">
    
        {{csrf_field()}}
        

        
        <section>   
        Choose program:
            <select name="sc" id="xaos">
        
            <optgroup label="postgraduates">
                @foreach($transport as $y)
                    <option value="{{$y->object_id}}">{{$y->object_name}}</option>
                @endforeach
            </optgroup>
        
            </select>
        </section>
    
    <br>

        <section>
        AM: 
            <input name='am' type='number' min="1000000" max="1999999" required="" oninvalid="this.setCustomValidity('1000000 < Value < 1999999')">
        </section>
        
        <br>
        
        <section>
        Select language:
            <select name="language" id="lang">
                    <option value="GR"> Greek</option>
                    <option value="EN"> English</option>
            </select>
        </section>
        
        <br>
        
        <section>
        <label for="upload-file">select csv file</label>
            <input type="file" name="upload-file" class="form-control">
        </div>
        <input class="btn btn-success" type="submit" value="Upload " name="submit">

        <section>
        <br>
            <input type='submit' value="Submit!">
        </section>
        
    </form>

(3) And the PdfController.php excerpt regarding form input data get is this:

public function job(Request $p)
    {
        $a1 = $p -> get('sc');  //works!
        $a2 = $p -> get('am');  //works!
        $a3 = $p -> get('language');  //works!
        
        //get csv file                          
        $upload = $p -> file('upload-file');
        $filePath = $upload ->getRealPath(); --> HELP NEEDED HERE!!
        
        //open and read file
        $file = fopen($filePath, 'r');
        $header = fgetcsv($file); //I want to insert data without header
        
        dd($header);

Please sign in or create an account to participate in this conversation.