masumluf's avatar

Laravel try catch issue.

Dear Developers,

I am getting 302 status code for following code

  try {
            $this->validate($request, [
                'select_file'  => 'required|mimes:xls,xlsx'
            ]);

            $path = $request->file('select_file')->getRealPath();

            $collections = fastexcel()->import($path);

            foreach ($collections as $collection) {
                //Exam::create($collection);
                $exam = new Exam();
                $exam->student_id = $collection['student_id'];
                $exam->teacher_id = $request->input('teacher_id');
                $exam->subject_name = $collection['subject_name'];
                $exam->mark = $collection['mark'];

                $exam->save();
            }
            return back()->with('success', 'Excel Data Imported successfully.');
        } catch (\Throwable $th) {
            return back();
        }

But If I use same code without try catch my Excel file uploads Successfully. I would like to want not to see any error at user interface thank you . Or Please give me any solution how to avoid any any laravel given error to hide and provide custome error instead it.

0 likes
6 replies
Sinnbeck's avatar

Try this to see the exception

} catch (\Throwable $th) {
            dd($th);
        }
Sinnbeck's avatar

Did you find the error. If so it might help others if you write what you discovered

willvincent's avatar

Throwable shouldn't be needed in a try catch, in fact laravel should handle such an error.

But the intent is to avoid laravel handling it automatically to prevent error output to the user...

I would like to want not to see any error at user interface thank you

Generally preventing error feedback isn't great, but everybody has their own business rules :D

Please or to participate in this conversation.