0

I'm using Jquery 3, and Laravel 5.8, and want to customize an error with JSON response from My controller.

I've tried to return response from controller, but it's appear default error message.

public function destroy($id)
    {
        $po = Po::findOrFail($id);
        $po->delete();

        if($po) {
            return response()->json([
                'message' => 'Product Owner Deleted',
                'data' => $po
            ]);
        } else {
            return response()->json([
                'msg' => 'Failed to delete a product owner / Po is assigned to project'
            ]);
        }
    }

Jquery

// Delete Data
    $('body').on('click', '#btn-destroy', function(event) {
        event.preventDefault();

        let me = $(this),
            url = me.attr('href'),
            csrf_token = $('meta[name=csrf-token]').attr('content');

        Swal.fire({
            title: 'Are you sure want to delete?',
            text: "Choose Option Wisely",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, Delete it!'
          }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: url,
                    method: 'POST',
                    data : {
                        '_token': csrf_token,
                        '_method': 'DELETE'
                    },
                    beforeSend: function () {
                        toastr.warning('Deleting Data...', 'WARNING');
                    },
                    success: function(data) {   
                        toastr.success(data.message, 'SUCCESS');
                        $('#datatable').DataTable().ajax.reload();
                    },
                    error: function(xhr) {
                        let error = xhr.responseJSON; 
                        toastr.error(error.msg, 'ERROR');
                    }
                });
            }
        });
    });

I want to return error like in condition, if true show this, if false show that. But, it always return error like :

" SQLSTATE[23000]: Integrity constraint violation: 1…(id)) (SQL: delete fromposwhereid` = 1 "

3
  • how does ur table and model looks like? i would be more specific on selecting record using where $po = Po::where('id', $id)->first(); Commented Aug 7, 2019 at 5:25
  • @Skeldar Model (pasted.co/7f15e4a1] Migration pasted.co/a654641c but.. it's still same when I use first(). Commented Aug 7, 2019 at 6:10
  • @HudaPrasetyo does your table is related to other table with Primary/Foreign key? Commented Nov 18, 2019 at 10:24

2 Answers 2

0

change your condition like that.

   $po = Po::findOrFail($id);

    if($po->delete()) {
        ...
    } else {
       ...
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Hmm.. still same results. It return default error message
please try this Query Builder like DB::table('pos')->where('id', $id)->delete();
truncate the table and do the delete using implicit model binding and check the status inside ajax request success function..
wait... can You explain more spesific, please ?, i didn;t get the point. :D
0
public function addCourse(Request $request)
 {
   $courseData = new Course();
   $courseSaved = $courseData->save();
   if ($courseSaved) {
     return response()->json(
       [
        'status' => 1,
        'message' => 'Course added successfully.'
       ]
     );
    } else {
       return response()->json(
          [
           'status' => 0,
           'message' => 'Something Was Wrong.'
          ]
        );
   }
 }
}

1 Comment

Please, insert comments in your answer in order to be clear for all. Thanks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.