Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In store try condition not working.

What I have tried:

PHP
public function store(Request $request)
   {
        $filtered=Expense::where('category',$request->category)->onlyTrashed()->first();
        if($filtered!==null)
        {
            expense::withTrashed()->where('id', $filtered['id'])->restore();
            return redirect()->route('expense.index')->with('success', "Expense category restored  successfully");

        }
        else{
           try {

           $validated = $request->validate([

               'category' => 'required|alpha|unique:expense_categories',
           ],
           [
               'category.unique' => 'This category is already exist'
           ]

       );

                   $expense = new Expense;
                   $expense->category = $validated['category'];

                   $expense->save();
                   return redirect()->route('expense.index')->with('success', "Expense category added successfully");
               }
               catch (\Exception $e) {
                   report($e);
                   return redirect()->back()->with('error', 'Failed to edit the category! Please try again.');
               }
        }

   }
Posted
Updated 4-Mar-22 9:07am
v2

1 solution

Check the (\) with Exception block in your code. Try like following:

try {
  // code that can throw exceptions
} catch(Exception $e) {
  // code that runs when an exception is caught
} finally {
  // code that always runs regardless of whether an exception was caught
}
 
Share this answer
 
Comments
Maciej Los 4-Mar-22 3:33am    
Hawk eye :)
+5!
M Imran Ansari 4-Mar-22 8:56am    
much required in development :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900