0

I'm trying to create a new service option while creating a bill (a bill can have many services), it's like creating a new tag, or category while writing post without reloading, but i have this internal server error 500, i think the problem is in the Controller because it works fine when i comment the create in controller, route and csrf and stuff are checked, thanks for helping me !

In my javascript :

var max_service_id = {{$max_service_id}};

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

$("#add_service_button").on("click",function(e){

e.preventDefault();

var service_name = $("input[name=service_name]").val();
var service_category = document.getElementById('service_category').value;
var service_description = $("input[name=service_description]").val();

$.ajax({
    type:'POST',
    url:'/service/add-in-bill',
    dataType: 'json',
    data:{
        name : service_name,
        category_id : service_category,
        description : service_description
    },
    success:function(data){
        if (data.message) {
            //This is error message
            alert(data.message);
        }
        if (data.success) {
            alert(data.success);
            var newService = new Option(service_name, max_service_id, false, true);
            $('.multipleSelect').append(newService).trigger('change');
            max_service_id++;
            $("#add_service_div").hide("fast");  
        }
    }
});

})
});

In my controller :

    $validator = Validator::make($request->all(), [
        'name' => 'required|unique:services',
        'category_id' => 'required',
    ]);
    if ($validator->fails()) {

        return response()->json(['message'=>$validator->errors()->all()]);

    } else {

        // It works fine when i comment this, so i think the problem is something about variables from javascript to php stuff
        Service::create($request->all());

        return response()->json(['success'=>'Create service success !']);

    }

// // I think the problem is in the Controller because it works fine when i comment the "Service::create($request->all());"

Rim
  • 168
  • 3
  • 9
  • When you get a 500-error, make sure you have error reporting turned on an check the web servers error log to find out what the actual error is. – M. Eriksson Jul 10 '19 at 05:19
  • You should enable debug so that you can have a better error report, 500 error could be anything. – catcon Jul 10 '19 at 05:20
  • Possible duplicate of [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – M. Eriksson Jul 10 '19 at 05:20
  • Oh and the error in console : – Rim Jul 10 '19 at 05:30
  • app.js:6 POST http://localhost:3000/service/add-in-bill 500 (Internal Server Error) – Rim Jul 10 '19 at 05:30
  • `Service::create($request->all());` when you do that make sure your `$request` contains the named keys same as is in your database. – danish-khan-I Jul 10 '19 at 05:35

0 Answers0