lara121081's avatar

Import CSV + Form inputs to same mysql table.

I'm using the laravel excel-package - https://docs.laravel-excel.com/3.1/imports/model.html

tl;dr: How can I push an imported csv file + form data to the same mysql table using the laravel-excel package?

Right now I have a form with a file upload input. I'm able to successfully upload a CSV file and push it to my database with the following code:

ComputerController:

    public function csv_import(Request $request) {
        Excel::import(new CsvImport, $request->file('file'));
        return back();
    }

CSV Import:

class CsvImport implements ToModel
{
    
    public function model(array $row)
    {
        return new Computer([
            'first_name' => $row[0],
            'last_name' => $row[1],
            'asset_tag' => $row[2],
        ]);

    }
}

What I want to do is the following: Have a form with one input and one file upload: e.g

 <input type="file" class="custom-file-input" id="inputFile" name="file">
<input type="text" class="form-control" id="pcname" name="pc_name">

Then upload your file in the form and fill out the pc_name input and have all the data pushed to the same table. I'm not sure how to do that. I tried passing $pc_name as a parameter into csv_import function like the following.

   public function csv_import(Request $request) {
        $pc_name = $request->pc_name;
        Excel::import(new CsvImport, $request->file('file'), $pc_name);
        return back();
    }

    public function model(array $row, $pc_name)
    {
        return new Computer([
            'first_name' => $row[0],
            'last_name' => $row[1],
            'asset_tag' => $row[2],
        'pc_name' => $pc_name
        ]);

        
    }

When I try to pass the function $pc_name, i get the following error:

Declaration of App\Imports\CsvImport::model(array $row, $pc_name) must be compatible with Maatwebsite\Excel\Concerns\ToModel::model(array $row)
0 likes
2 replies
kevinbui's avatar
kevinbui
Best Answer
Level 41

Because you can access the request object any where in your project. This may work:

class CsvImport implements ToModel
{
    
    public function model(array $row)
    {
        return new Computer([
            'first_name' => $row[0],
            'last_name' => $row[1],
            'asset_tag' => $row[2],
            'pc_name' => request('pc_name');
        ]);

    }
}

Or, you can pass the pc name to the CsvImport instance.

class CsvImport implements ToModel
{
    protected $pcName;

    public function __construct(string $pcName) 
    {
        $this->pcName = pcName;
    }
    
    public function model(array $row)
    {
        return new Computer([
            'first_name' => $row[0],
            'last_name' => $row[1],
            'asset_tag' => $row[2],
            'pc_name' => $this->pcName;
        ]);

    }
}

Then in your controller:

public function csv_import(Request $request) {
        Excel::import(
            new CsvImport($request->input('pc_name')), 
            $request->file('file')
        );

        return back();
    }
lara121081's avatar

Awesome, thank you! This worked.

'pc_name' => request('pc_name') 

Please or to participate in this conversation.