0

I need your help. I don't know how to import the excel file. I mean I don't understand where to put this users.xlsx and how to get its directory

 public function import()
        {
            Excel::import(new UsersImport, 'users.xlsx');

            return redirect('/')->with('success', 'All good!');
        }
alphal
  • 149
  • 3
  • 3
  • 14
  • you have to upload a xls file and then import – Sagar Gautam May 03 '19 at 13:00
  • where to upload the file , i mean in which folder ? – alphal May 03 '19 at 13:01
  • I mean through file upload in form if file is dynamic. if your xls file is not dynamic then add the file inside public directory and access it with public_path() helper function – Sagar Gautam May 03 '19 at 13:02
  • I am trying to put it int public directory : Excel::import(new UsersImport, request()->file(public_path('users.xlsx'))); but it says storage/logs/laravel-2019-05-03.log" could not be opened: failed to open stream: Permission denied – alphal May 03 '19 at 13:06
  • 2
    it's about file permission issue check that – Sagar Gautam May 03 '19 at 13:07
  • is there a way i can get it from desktop please ? – alphal May 03 '19 at 13:19
  • Your permission denied error means the permissions on the `storage` or `storage/logs` folder isn't correct. See https://stackoverflow.com/questions/30639174/how-to-set-up-file-permissions-for-laravel-5-and-others. – ceejayoz May 03 '19 at 13:29
  • Now it says Interface 'Maatwebsite\Excel\Concerns\ToModel' not found – alphal May 03 '19 at 13:39
  • You are missing the "use" statement in your UsersImport class. Also, I believe the default behavior is to check the storage directory for the file if you use it the way you did in your example. – Jason Houle May 03 '19 at 17:45

1 Answers1

2

its simple on mattwebsite you need a controller like below :

  public function importExcel(Request $request)
{
    if ($request->hasFile('import_file')) {
        Excel::load($request->file('import_file')->getRealPath(), function ($reader) {
            foreach ($reader->toArray() as $key => $row) {
// note that these fields are completely different for you as your database fields and excel fields so replace them with your own database fields
                $data['title'] = $row['title'];
                $data['description'] = $row['description'];
                $data['fax'] = $row['fax'];
                $data['adrress1'] = $row['adrress1'];
                $data['telephone1'] = $row['telephone1'];
                $data['client_type'] = $row['client_type'];


                if (!empty($data)) {
                    DB::table('clients')->insert($data);
                }
            }
        });
    }

    Session::put('success on import');

    return back();
}

and a view like this :

                <form
                  action="{{ URL::to('admin/client/importExcel') }}" class="form-horizontal" method="post"
                  enctype="multipart/form-data">
                {{ csrf_field() }}
                <div class="form-group">
                    <label class="control-label col-lg-2">excel import</label>
                    <div class="col-lg-10">
                        <div class="uploader"><input type="file"  name="import_file" class="file-styled"><span class="action btn btn-default legitRipple" style="user-select: none;">choose file</span></div>
                    </div>
                </div>
                <button class="btn btn-primary">submit</button>
            </form>

and finally a route like below :

Route::post('admin/client/importExcel', 'ClientController@importExcel');
Farshad
  • 1,830
  • 6
  • 38
  • 70
  • It says to me "Undefined index: name" – alphal May 03 '19 at 13:34
  • @alphal consider that you should have the name both in database and in excel file and those fields as you may know are here $data['name'] = $row['name']; – Farshad May 03 '19 at 13:40