andylord565's avatar

uploading CSV

View:

@extends('layouts.suplayout')

@section('content')
<h1>CSV Upload</h1>

@include ('errors.list')
{!! Form::open(array('method' => 'POST','url' => 'uploadexcel')) !!}
<div class="form-group">
{!! Form::file('file') !!}
</div>
<div class="form-group">
{!! Form::submit('Add', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}



@stop

controller method:

public function csvupload()
{
return view('superadmin.csvupload');

}
public function upload()
{
    $rules = array(
        'file' => 'required',
        //'num_records' => 'required',
    );

    $validator = Validator::make(Input::all(), $rules);
    // process the form
    if ($validator->fails()) 
    {
        return Redirect::to('sales')->withErrors($validator);
    }
    else 
    {
        try {
            Excel::load(Input::file('file'), function ($reader) {

                foreach ($reader->toArray() as $row) {
                    sales::Create($row);
                }
            });
            \Session::flash('success', 'Sales uploaded successfully.');
            return redirect(route('sales'));
        } catch (\Exception $e) {
            \Session::flash('error', $e->getMessage());
            return redirect(route('sales'));
        }
    } 
} 

routes:

Route::get('superadmin/csvupload', 'SuperAdminController@csvupload');
Route::post('uploadexcel', 'SuperAdminController@upload');

I am doing the above and uploading a CSV however when doing so it brings no errors nothing anyone know why this is happening or how to fix this??

Edit:

Just found this in the debugbar: error Could not open for reading! File does not exist. For some reason my errors aren't popping up weird , anyone know why it cant read it?

0 likes
4 replies
Snapey's avatar

your form element needs enctype="multipart/form-data" to allow file upload

1 like
andylord565's avatar

Thanks @Snapey its now uploading (always something simple) and adding them however the fields are blank

Snapey's avatar

have you created a $fillable array in your sales model?

see the docs for mass assignment

1 like
andylord565's avatar

@Snapey yes i have i just figured it out i had not added the titles in the csv so it could not read it Thanks for the help

Please or to participate in this conversation.