简体   繁体   中英

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. While importing the row from excel file, it will also insert value form input field. Suppose I have an input filed for price. And input field for excel file. Excel files contains 100 rows. 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:

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

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. Can you please help me to modify my code.

Modify by passing price into the 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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