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

Leff7's avatar
Level 4

Laravel 5.4 - check if all model inserts and updates went ok

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
        ]);
      }
    }
  }
0 likes
1 reply
AddWebContribution's avatar

You should specify redirect route with Success Message after foreach complete like,

return view('home')->withFlashSuccess('File was successfully uploaded.');

Please or to participate in this conversation.