I am working on a application, in which the users can extract data from excel file to MySQL DB. The application will check every row of ID collunm from excel file with content_id table of the database. If any ID matches any record of DB, then the row will update. Otherwise it will create a new record in DB. This is the code of my function in controller:
public function upload(Request $request)
{
if($request->hasFile('excel')){
Excel::load($request->file('excel')->getRealPath(), function ($reader) {
foreach ($reader->toArray() as $key => $row) {
foreach ($row as $record => $info){
if ($content = Content::find($info['id'])) {
$content->views()->save($info['views']);
}
else{
$content = new Content();
$content->title = $info['title'];
$content->content_id = $info['id'];
//$content->views()->save($info['views']);
$content->save();
}
}
}
});
}
return redirect()->back();
}
But it shows error as like below:
I have tried to find the error by using dd()
and print_r()
. But cannot find what is the problem. Along with this, I am also confused about the quality of my code as I am using foreach loop in another foreach loop. So, does anyone have any solution? Thanks in advance. I am using Maatwebsite\Excel to handle excel import/export etc.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire