php - Ajax returning undefined in laravel

First of all, I am a beginner in using ajax inside laravel. I tried to check the input field while it's working or not through an alert or console.log it's returning undefined.

my blade file code

<div id="add_tag_modal" class="modal fade">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body">
                <h2>Add New Tag</h2>
                <hr>
                <form id="add_tag_form" method="POST">
                    @csrf
                    <div class="mess"></div>
                    <div class="form-group">
                        <label for="">Name</label>
                        <input name="name" type="text" class="form-control">
                    </div>
                    <div class="form-group">
                        <input class="btn btn-primary btn-sm" type="submit" >
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

my controller file code..though its commented public function store(Request $request) { // Tag::create([

    //     'name' => $request -> name

    // ]);
}

web.php /**

  • tag controller */

Route::resource('tag','App\Http\Controllers\TagController');

Route::post('tag-create','App\Http\Controllers\TagController@store')->name('tag.create');

lastly my ajax code

  $(document).on('submit','form#add_tag_form',function(e){

    e.preventDefault();


    let name = $('form#add_tag_form input[name="name"]').val();


    if (name == '') {

        $('.mess').html('<p class="alert alert-danger">All fields are required!<button type="button" class="close" data-dismiss="alert" aria-label="Close">');

    }else{

         $.ajax({
    url:'tag-create',
    method: "POST",
    contentType: false,
    processData : false,
    data : new FormData(this),
    success: function(data){
        console.log(data.name);

    }
         });
    }

  });

Answer

Solution:

I believe you need to update your controller.

You need to validate the request first

$validate = $this->validate($request, [
    'name' => 'required|string|max:255'
]);

after that, send the $validate variable to model Tag to create a new resource:

$save = Tag::create($validate);

if($save) {
   return true; //return statements you can customize based on your requirement
} 
return false;

By doing this you are validating the incoming request and if your return value is true process further. But be carefull with response you should return the message and status code to validate the response.. Don't use true or false for return statement. In AJAX response, you check the status code and proceed further with the message.

Thank you

Answer

Solution:

You should try to replace 'let name' into 'var name' then alert or console it.

Answer

Solution:

You can try adding this to your php code to show the error message of the 500 code.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Source