【问题标题】:How to insert data from excel file with text input field in Laravel如何在 Laravel 中使用文本输入字段从 excel 文件中插入数据
【发布时间】:2021-11-18 20:38:27
【问题描述】:

我想将 excel 文件中的数据插入数据库。从 excel 文件导入行时,它还将插入值表单输入字段。假设我有一个价格输入。和excel文件的输入字段。 Excel 文件包含 100 行。所以在导入时,每一行都会插入价格文本。

刀片文件:

<input type="text" name="price" />
<input type="file" name="bulk_file" />

控制器:

 public function bulk_upload(Request $request)
{
    if($request->hasFile('bulk_file')){
        $import = new ProductsImport;
        Excel::import($import, request()->file('bulk_file'));
    }
    return back();
}

型号:

    class ProductsImport implements ToCollection, WithHeadingRow, WithValidation, ToModel
   {
    private $rows = 0;
    public function collection(Collection $rows) {
        $canImport = true;
        if($canImport) {
            foreach ($rows as $row) {
                $productId = Product::create([
                            'name' => $row['name'],
                            'added_by' => Auth::user()->user_type == 'seller' ? 'seller' : 'admin',
                            'user_id' => Auth::user()->user_type == 'seller' ? Auth::user()->id : User::where('user_type', 'admin')->first()->id,
                            'category_id' => 24,
                            'brand_id' => !empty($row['brand_id']) ? $row['brand_id']: 0,
                            'price' => request('price');
                ]);
            }

            flash(translate('Products imported successfully'))->success();
        }

    }

我正在使用 maatwebsite/excel 包。 你能帮我修改我的代码吗?

【问题讨论】:

    标签: laravel eloquent excel-import


    【解决方案1】:

    通过将价格传递给 ProductsImport 类进行修改:

        class ProductsImport implements ToCollection, WithHeadingRow, WithValidation, ToModel
       {
        protected $price;
    
        function __construct($price) {
            $this->price = $price;
        }
        private $rows = 0;
        public function collection(Collection $rows) {
            $canImport = true;
            if($canImport) {
                foreach ($rows as $row) {
                    $productId = Product::create([
                                'name' => $row['name'],
                                'added_by' => Auth::user()->user_type == 'seller' ? 'seller' : 'admin',
                                'user_id' => Auth::user()->user_type == 'seller' ? Auth::user()->id : User::where('user_type', 'admin')->first()->id,
                                'category_id' => 24,
                                'brand_id' => !empty($row['brand_id']) ? $row['brand_id']: 0,
                                'price' => $this->price
                    ]);
                }
    
                flash(translate('Products imported successfully'))->success();
            }
    
        }
    

    在控制器中:

    public function bulk_upload(Request $request)
    {
        if($request->hasFile('bulk_file')){
            $import = new ProductsImport($request->price);
            Excel::import($import, request()->file('bulk_file'));
        }
        return back();
    }
    

    【讨论】:

    • 新 ProductsImport($request->price);如何在请求中传递多列。假设我有 price、value、options、user_id 列。
    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2011-04-11
    • 1970-01-01
    • 2020-07-13
    相关资源
    最近更新 更多