Reputation: 511
I am trying to Import an Excel File into DB, after checking File Format, which I have done successfully, the Only Part where I am getting a problem is returning back on my view page with error or success message.
My Controller::
Excel::load(Input::file('datafile'), function ($reader) use($attr_array) {
...
//IF DATA MATCH
if($data_match == 'true')
{
...
foreach ($reader->toArray() as $row) {
$Pdt_data->slug = Util::uniqueSlug($row['attr_name_value'], 'Product');
$rem_value['slug'] = $Pdt_data->slug;
$resultant = array_merge($rem_value, $row);
//INSERT INTO DATABASE
Product::firstOrCreate($resultant);
}
\Session::flash('success', 'Data uploaded successfully.');
return Redirect::back();
}
//IF DATA DOES NOT MATCH
else
{
\Session::flash('error', 'You Are Trying To Import File Of Different Category!');
return Redirect::back();
}
});
I had no help with Duplicate questions, Any help would be highly appreciated.
Upvotes: 3
Views: 1820
Reputation: 511
I have finally solved this problem myself by :
Updated Code:
$importstatus = ''; //DEFINING VARIABLE OUTSIDE EXCEL FUNCTION
Excel::load(Input::file('datafile'), function ($reader) use($attr_array) {
...
//IF DATA MATCH
if($data_match == 'true')
{
...
foreach ($reader->toArray() as $row) {
$Pdt_data->slug = Util::uniqueSlug($row['attr_name_value'], 'Product');
$rem_value['slug'] = $Pdt_data->slug;
$resultant = array_merge($rem_value, $row);
//INSERT INTO DATABASE
Product::firstOrCreate($resultant);
}
\Session::flash('success', 'Data uploaded successfully.');
$importstatus = 'fine'; //SETTING A VARIABLE VALUE
}
//IF DATA DOES NOT MATCH
else
{
\Session::flash('error', 'You Are Trying To Import File Of Different Category!');
$importstatus = 'error'; //SETTING A VARIABLE VALUE
}
});
//CHECK VARIABLE VALUE FOR DECISION
if ($importstatus == 'fine') {
\Session::flash('success', 'Data uploaded successfully.');
return Redirect::back();
} else {
\Session::flash('error', 'You are trying to import file of Different Category!');
return Redirect::back();
}
Upvotes: 3