Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mahmudcdh's avatar

Help to insert data from an excel file to 3 tables (MySql Database) using laravel10 & maatwebsite/excel

Database Table name & schema -

  1. vessel table schema: id, vessel, voyage, vessel_flag, vessel_departure_date, vessel_arrival_date, vessel_pol, vessel_pod, user_id [Note: user_id is foreign key reference with user table id]

  2. bl table schema: id, vessel_id, mlo, bol_reference, line_no, bol_type [Note: vessel_id is foreign key reference with vessel table id]

  3. container table schema: id, vessel_id, bl_id, container_num, sz_tp, num_of_pkgs, status, seal_number, gross_weight [Note: vessel_id is foreign key reference with vessel table id, bl_id is foreign key reference with bl table id ]

I have an excel file (sample share link: https://docs.google.com/spreadsheets/d/1WyH-VTAbAk1yiI7kn8BSIx0MhSp-3WJi/edit?usp=drive_link&ouid=107846359291251277373&rtpof=true&sd=true )

Would be much appreciated, Can anyone please let me advise / know how to upload this file into above mentioned 3 table files using laravel10 & maatwebsite excel

0 likes
1 reply
JabatoForever's avatar

hi @mahmudcdh run the command to create an import

php artisan make:import YourImportClass

then in the import class implement the model function as shown adding the rest of columns

// app/Imports/YourImportClass.php
 
namespace App\Imports;
 
use Maatwebsite\Excel\Concerns\ToModel;
 
class YourImportClass implements ToModel
{
    public function model(array $row)
    {
        // Define how to create a model from the Excel row data
        return new YourModel([
            'id' => $row[0],
            'vessel_id' => $row[1],
           // and so n for each column in the give excel row 
        ]);
    }
}

Please or to participate in this conversation.