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

SUNDAE-SOUP's avatar

Post additional data along with the excel import file in laravel via Laravel Excel

Hello Everyone, I need some help on how I can pass the data from the controller going to the import.php file so that it can be post along side with the data that is inside the excel file upon import. Here's my Controller:

public function import (Request $request) {

    $branchId = Branch::where('branch_name', $request->input('branch'))->value('id');
    $monthId = Month::where('name', $request->input('months'))->value('id');
    $yearId = Year::where('name', $request->input('years'))->value('id');


    Excel::import(new ReportsImport($branchId, $monthId, $yearId), $request->file(key: 'import_file'));
    return 'Success';

=====================================================================

My Import file:

use App\Models\Report; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Concerns\WithHeadingRow; /* use Maatwebsite\Excel\Imports\HeadingRowFormatter; HeadingRowFormatter::default('none'); */

class ReportsImport implements ToModel, WithHeadingRow {

private $branchId;
private $monthId;
private $yearId;

public function __construct($branchId, $monthId, $yearId)
{
    $this->branchId = $branchId;
    $this->monthId = $monthId;
    $this->yearId = $monthId;
}

/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/

public function model(array $row)
{
    $branchId = $this->branchId;
    $monthId = $this->monthId;
    $yearId = $this->yearId;
    

    return new Report([
        "branch_id" => $branchId->id ?? NULL, 
        "month_id" => $monthId->id ?? NULL,
        "year_id" => $yearId->id ?? NULL,
        "opening_date" => $row['opening_date'],
        "orders_session" => $row['orders_session'],
	]);

=======================================================================

Now, in my database, the import process turns out to be successful but the $branchId, $monthId and $yearId datas are not being fetch successfully from the controller. I'm sure that the data is being pass correctly in my controller from the form but my problem is I don't know how I'm going to call the data in my import.php. I hope someone can help. Thank you!

0 likes
3 replies
ajithlal's avatar

https://laravel.com/docs/10.x/queries#retrieving-a-single-row-column-from-a-table

As per the doc $this->branchId will be the id of the branch (mostly an integer value). You are trying to get the id from an integer value which will return null (not sure why you didn't get an error instead). Try to die and dump the private properties inside the model function on the ReportsImport class and check the type of the values and make sure you are getting the values inside the class.

SUNDAE-SOUP's avatar

@ajithlal Hello there! Thanks for your time answering. I tried dd but it returns null as value of those variables. which means that it didn't get the value which is exactly my problem.

SUNDAE-SOUP's avatar
SUNDAE-SOUP
OP
Best Answer
Level 1

Solved the problem by editing this:

$branchId = Branch::where('branch_name', $request->input('branch'))->value('id');
$monthId = Month::where('name', $request->input('months'))->value('id');
$yearId = Year::where('name', $request->input('years'))->value('id');

into this:

	$branchId = Branch::where('id', $request->input('branch'))->value('id');
    $monthId = Month::where('id', $request->input('month'))->value('id');
    $yearId = Year::where('id', $request->input('year'))->value('id');

now, from here, I am able to get the data and pass the variable into my import.php

Please or to participate in this conversation.