Level 42
You should specify redirect route with Success Message after foreach complete like,
return view('home')->withFlashSuccess('File was successfully uploaded.');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am updating and inserting from a file into 3 tables in DB. I would like to check if everything went ok, if no errors were thrown, so that I can show a success message to a user if the upload went ok. How can I do that?
This is my code:
public function upload(Request $request)
{
if ($request->hasFile('csvFile')) {
$file = $request->file('csvFile');
$file->move('uploads', $file->getClientOriginalName());
$this->store($file);
}
Session::flash('flash_message','File was successfully uploaded.');
return view('home');
}
public function store($file)
{
$path = base_path('public/uploads/' .$file->getClientOriginalName());
$rows = Excel::load($path, function($reader) { })->get()->toArray();
foreach($rows as $row) {
$id = $row[4];
$shopeTitle = $row[1];
$price = $row[6];
$mall = $row[5];
$visitors = $row[3];
Shop::updateOrCreate(
['id' => $id],
['title' => $shopeTitle]
);
Mall::where('id', $id)->update([
'price' => $price,
'visitors' => $visitors
]);
if ($mall != 41 || $mall!= 42) {
DB::table('store_mall')->insert([
'mall' => $price,
'store' => $id
]);
}
}
}
Please or to participate in this conversation.