0

I can't return a toast error message. Where am I making a mistake? Message returns when successful.

My code is as follows:

public function store(Request $request)
{
    $validated = $request->validate([
        'category_name' => 'required|unique:categories|max:50',
    ]);

    $data = array();
    $data['category_name'] = $request->category_name;

    $save = DB::table('categories')->insert($data);
    if ($save) {
        Toastr::success('Post Successfully Saved :)', 'Success');

        return redirect()->route('admin.category');
    } else {
        Toastr::error('Error :)', 'Error');

        return redirect()->route('admin.category');
    }
}

3 Answers 3

0

Try the following:

public function store(Request $request)
{
    $validated = $request->validate([
        'category_name' => 'required|unique:categories|max:50',
    ]);

    $data = array();
    $data['category_name'] = $request->category_name;

    $save = DB::table('categories')->insert($data);
    if (!$save) {
        Toastr::error('Error :)', 'Error');
        
        return redirect()->route('admin.category');
    } else {
        Toastr::success('Post Successfully Saved :)', 'Success');
        
        return redirect()->route('admin.category');
    }
}
2
  • Bro thanks but not working. Only work success method. Commented Apr 19, 2021 at 23:02
  • ok try if ($validator->fails()) { } rather than if(!$save) Commented Apr 19, 2021 at 23:41
0

You can try this code.. You can use ->fails() function to check the inputs

public function store(Request $request)
    {
        $validated = $request->validate([
            'category_name' => 'required|unique:categories|max:50',
        ]);
         if($validated->fails()){
          Toastr::error('Error :)','Error');
          return redirect()->route('admin.category');
         }
        $data=$request->only(['category_name']);
        $save = DB::table('categories')->insert($data);
        Toastr::success('Post Successfully Saved :)','Success');
        return redirect()->route('admin.category');
}
0

I found the solution . I created a validator myself

public function store(Request $request)
    {
        $validated = Validator::make($request->all(), [ 
            'category_name' => 'required|unique:categories|max:50',

        ]);
        $notificationerror=array(
            'messege'=>'Category Added Error',
            'alert-type'=>'error',
            'positionClass' =>'toast-top-right'
        );
        if($validated->fails()){
            return redirect()->route('admin.category')->with($notificationerror);

        }

        $data=array();
        $data['category_name']=$request->category_name;
        DB::table('categories')->insert($data);
        $notification=array(
            'messege'=>'Category Added Successfully',
            'alert-type'=>'success',
        );
        return redirect()->route('admin.category')->with($notification);

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.