0

Hi am using laravel excel to import file from excel and getting the data but the column heading is getting changed and i need to keep is as same. For eg: column name is Mat_Type-1 then it get convert to mat_type_1.

Whereas i want to keep it as same to get some data out of the column.

Function in controller :

public function uploadexcel(Request $request){
// $this->validate($request, [
//  'importfile'  => 'required|mimes:xls,xlsx'
// ]);
    echo $path = $request->file('importfile');

    $array = Excel::toArray(new ProductsImport, $path);


    print_r($array);
}

Excel\Imports class:

namespace App\Imports;

  use App\Models\Admin\CarVariant;
  use App\Models\Admin\Product;
  use Illuminate\Support\Carbon;
  use Illuminate\Support\Facades\DB;
  use Illuminate\Validation\Rule;
  use Maatwebsite\Excel\Concerns\ToModel;
  use Maatwebsite\Excel\Concerns\WithBatchInserts;
  use Maatwebsite\Excel\Concerns\WithChunkReading;
  use Maatwebsite\Excel\Concerns\WithHeadingRow;
  use Maatwebsite\Excel\Concerns\WithValidation;
 class ProductsImport implements ToModel,WithHeadingRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
            // dd($row);

}

}

Any help is deeply appreciated. Tried multiple option but not working.

  • @ahackney Column name are dynamic and it will change want to read that column name and use the same. Got an option of HeadingRowFormatter but confused how to implement it. – Santosh Upadhyay Jun 09 '20 at 07:12

2 Answers2

0

Try out this. It's from a live project. I'm saving the file to a folder and getting the data in array then pressing it to datatable. No change in data-table name.

use App\Imports\ProductsImport;
use Maatwebsite\Excel\Facades\Excel;

public function import(Request $request)
{
    //Save the File
    if($request->hasFile('file'))
    {
        // Get the file with extension
        $filenameWithExt = $request->file('file')->getClientOriginalName();
        //Get the file name
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        //Get the ext
        $extension = $request->file('file')->getClientOriginalExtension();
        //File name to store
        $fileNameToStore = $filename.'_'.time().'.'.$extension;
        //Upload File
        $path = $request->file('file')->storeAS('public/excels', $fileNameToStore);


    }


    $this->validate($request, [
        'file' => 'required|mimes:xlsx,xls',
    ]);

    $products = Excel::toArray(new ProductsImport(), $request->file('file'));

    foreach($products[0] as $row) {
        //  dd($row[1].' '.$row[2]);
        $arr[] = [
            // If uncomment this id from here, remove [0] from foreach
            // 'id' => $row[0], 
            'image' => $row[1],
            'title' => $row[2],
            'slug' => $row[3],
            'text' => $row[4],
            'brand_id' => $row[5],
            'category_id' => $row[6],
        ];
    }

    if(!empty($arr)){
        DB::table('products')->insert($arr);
    }

    return back()->with('success', 'Excel file uploaded to database successfully');

}
Nargesh Rana
  • 134
  • 1
  • 15
  • Column name are dynamic and it will change want to read that column name and use the same. Got an option of HeadingRowFormatter but confused how to implement it – Santosh Upadhyay Jun 09 '20 at 07:12
0

So whatever you're doing with the row this should work.

I would assume for each row of the excel file you're creating a new model.

 public function model(array $row)
    {
        return new Whatever([
            'Mat_Type-1'  => $row['mat_type_1'],
            'Column_Name-1'  => $row['column_name_1'],
            'Column_Name-2'  => $row['column_name_2'],
        ]);
    }
ahackney
  • 534
  • 6
  • 14
  • Add in your import file this line use Maatwebsite\Excel\Concerns\Importable; and implenets it like this class Import implements ToModel,WithHeadingRow – Nasser Albelbeisi Mar 13 '23 at 14:25