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!