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

1.2k Views Asked by At

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.

1

There are 1 best solutions below

1
Ali Mesbahi On

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