我正在try 使用Maatwebsite-excel版本3.1导入Laravel版本5.7中的.xlsx文件.我想要实现的是跳过文件的第一行,以避免在数据库中导入列标题.

I've tried to use version 2 syntax, calling the skip() method.

public function voter_import(Request $request)
{
    if (empty($request->file('file')->getRealPath())) 
    {
        return back()->with('success','No file selected');
    }
    else 
    {
        Excel::import(new VotersImport, $request->file('file'))->skip(1);
        return response('Import Successful, Please Refresh Page');
    }
}

class VotersImport implements ToModel
{
public function model(array $row)
   {
    return new Voter([
      'fname'          =>  $row[0],
      'lname'          =>  $row[1],
      'phone'          =>  $row[2],
      'gender'         =>  $row[3],
      'state'          =>  $row[4],
      'occupation'     =>  $row[5],
      'address'        =>  $row[6],
      'vin'            =>  $row[7],
      'dob'            =>  $row[8],
      'campaign_id'    =>  $row[9],
    ]);
   }
}

error message:

Call to undefined method Maatwebsite\Excel\Excel::skip()

推荐答案

you can implement the StartingRow

use Maatwebsite\Excel\Concerns\WithStartRow;

class VotersImport implements ToModel, WithStartRow
{
    /**
     * @return int
     */
    public function startRow(): int
    {
        return 2;
    }
}

Another option would be to use HeadingRow https://docs.laravel-excel.com/3.1/imports/heading-row.html

Laravel相关问答推荐

如何定制有关密码被泄露的Laravel Breeze S消息?

如何在自定义表单请求中获取错误验证数据

未找到 apiResource 404 中的变量

Laravel Debugbar 中的 Laravel api routes路由

在 laravel 项目中放置 css 文件的位置

如何在 laravel 5.3 中验证没有 auth:api 中间件的用户?

如何在 Laravel Passport 中获取刷新令牌?

laravel 关系集合上的自定义排序

Laravel Blade:@stop VS @show VS @endsection VS @append

调用未定义函数 Illuminate\Encryption\openssl_decrypt()

Laravel 5.7 判断Electron邮件是否经过验证

如何更改 Laravel5 中的视图文件夹?

如何使用 Laravel 模型访问数据库视图?

eloquent的搜索/自定义属性的位置

Laravel 控制器构造

Laravel 规则和正则表达式 (OR) 运算符的问题

配置和测试 Laravel 任务调度

Laravel 验证存在于不存在的地方

Eloquent - 带有字符串值而不是列标题的连接子句

Laravel s3 多桶