Use dd to see your row
public function model(array $row)
{
dd($row);
Hi, I am uploading excel and want to insert the data in database. I have used https://laravel-excel.com/ package for this Here is my excel image, for header and data :https://ibb.co/KDh2m61
But while uploading I got an error Undefined array key 1
uploadcontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\OrdersImport;
use Maatwebsite\Excel\Facades\Excel;
class UploadController extends Controller
{
public function index()
{
//return a form to upload excel file
return view('index');
}
public function store()
{
Excel::import(new OrdersImport, request()->file('order_file'));
return 'uploaded!';
}
}
Orderimport.php
<?php
namespace App\Imports;
use App\Models\Order;
use Maatwebsite\Excel\Concerns\ToModel;
class OrdersImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Order([
'order' => $row[0],
'order_date' => $row[1],
'order_qty' => $row[2],
'sales' => $row[3],
'ship_model' => $row[4],
'profit' => $row[5],
'unit_price' => $row[6],
'customer_name' => $row[7],
'customer_segment' => $row[8],
'product_category' => $row[9],
]);
}
}
dd($rows) gives this
^ array:10 [▼
0 => "Order ID"
1 => "Order Date"
2 => "Order Quantity"
3 => "Sales"
4 => "Ship Mode"
5 => "Profit"
6 => "Unit Price"
7 => "Customer Name"
8 => "Customer Segment"
9 => "Product Category"
]
Order.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasFactory;
protected $guarded = ['id'];
}
orders Migration file
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->string('order');
$table->string('order_date');
$table->string('order_qty');
$table->string('sales');
$table->string('ship_model');
$table->string('profit');
$table->string('unit_price');
$table->string('customer_name');
$table->string('customer_segment');
$table->string('product_category');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
};
Thanks
@david001 You are missing the WithHeadingRow
<?php
namespace App\Imports;
use App\Models\Order;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class OrdersImport implements ToModel, WithHeadingRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
dd($row);
return new Order([
'order' => $row['Order ID'],
'order_date' => $row['Order Date'],
'order_qty' => $row['Order Quantity'],
'sales' => $row['Sales'],
'ship_model' => $row['Ship Mode'],
'profit' => $row['Profit'],
'unit_price' => $row['Unit Price'],
'customer_name' => $row['Customer Name'],
'customer_segment' => $row['Customer Segment'],
'product_category' => $row['Product Category'],
]);
}
}
Please or to participate in this conversation.