https://docs.laravel-excel.com/3.1/imports/multiple-sheets.html#conditional-sheet-loading
do them as you said, 2 separate imports.
Are you trying to add each sheet into a different database table? then that is the way to go
I'm testing the possibility of uploading data from 2 sheets from an excel file. The two sheets contain different data, so they need to be considered as different imports. The instructions that i fund at https://docs.laravel-excel.com/3.1/imports/multiple-sheets.html are a little confusing. Does anyone have a practical example on how to do this? Thanks for the help.
I have a excel spreadsheet with 2 sheets. names and products
You need a class for each sheet. NamesImport and ProductsImport
You need another class to assign sheet names to each import NamesProductsImport
I have a database with 2 tables names and products.
<?php
namespace App\Imports;
use App\Models\Name;
use Maatwebsite\Excel\Concerns\ToModel;
class NamesImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Name([
'first-name' => $row[0],
'last-name' => $row[1],
]);
}
}
<?php
namespace App\Imports;
use App\Models\Product;
use Maatwebsite\Excel\Concerns\ToModel;
class ProductsImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Product([
'company' => $row[0],
'quantity' => $row[1],
]);
}
}
Now you need a class to run each import as a separate sheet notice the sheet names.
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class NamesProductsImport implements WithMultipleSheets
{
public function sheets(): array
{
return [
'names' => new NamesImport(),
'products' => new ProductsImport(),
];
}
}
The controller calls the NamesProductsImport. that calls each separate import by sheet.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\NamesProductsImport;
use Maatwebsite\Excel\Facades\Excel;
class NamesProductsController extends Controller
{
public function import()
{
Excel::import(new NamesProductsImport, base_path('names_products.xlsx'));
return redirect('/')->with('success', 'All Good!');
}
}
here are the migrations
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNamesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('names', function (Blueprint $table) {
$table->id();
$table->string('first-name');
$table->string('last-name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('names');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('company');
$table->integer('quantity');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
works like a charm.
Please or to participate in this conversation.