mercredi 6 novembre 2019

How to add catch up error message in laravel

THE PROBLEM NOW IS I WANT TO LET THE USER READ THE ERROR MESSAGE AS ( DUPLICATE CODE ) SO FAILED TO ADD DATA INTO THE DATABASE

HERE IS THE CODE

 public function store(Request $request)
{

    $this->validate($request, [

        'code' => 'required',
        'description' => 'required',
        'parent_id' => 'required',
        'status' => 'required',
    ]);


    $category = new Category([
        'id' => $request->get('id'),
        'code' => $request->get('code'),
        'description' => $request->get('description'),
        'parent_id' => $request->get('parent_id'),
        'status' => $request->get('status'),
    ]);


    $category->save();
    return redirect()->route('category.create')->with('success', 'Data has been successfully added into the system');
}

ANOTHER CODE I WOULD LIKE TO USE

      try {
            $category = Category::find($request);
            $category->delete();
        } catch (\Illuminate\Database\QueryException$e) {
            if ($e->getCode() == '1602') {
                $status1 = 'failed';
                $statusMsg1 = 'failed to add, duplicate code was found!';
                return redirect()->back()->with($status1, $statusMsg1);
            }
        }

i WANT TO ADD THIS CODE INTO , BUT FAILED TO DO SO



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire