简体   繁体   English

如何使用 Laravel 中的文本输入字段从 excel 文件中插入数据

[英]How to insert data from excel file with text input field in Laravel

I want to insert data from an excel file into the database.我想将 excel 文件中的数据插入数据库。 While importing the row from excel file, it will also insert value form input field.从 excel 文件中导入行时,它还将在输入字段中插入值。 Suppose I have an input filed for price.假设我有一个价格输入。 And input field for excel file. excel 文件的输入字段。 Excel files contains 100 rows. Excel 文件包含 100 行。 So when importing this, every rows will insert with the price text.所以当导入这个时,每一行都会插入价格文本。

Blade File:刀片文件:

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

Controller: Controller:

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

Model: Model:

    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();
        }

    }

I'm using maatwebsite/excel package.我正在使用 maatwebsite/excel package。 Can you please help me to modify my code.你能帮我修改我的代码吗?

Modify by passing price into the ProductsImport class:通过将价格传递到 ProductsImport class 进行修改:

    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();
        }

    }

and in the controller:在 controller 中:

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM