In this instance I was missing a column on my database table. I was assigning a variable to a column that didn’t exist. Despite debugging the code and removing lines that caused the error and checking each column with those in my code, I still skipped the missing column. At the time I had my AJAX set up and it was difficult to see the response I was given through the error function as it was not formatted; each line was following the next. But once I had exhausted my trial and error and deductive reasoning, I went over the error response line by line to find that the email was the missing column. It didn’t occur to me that the missing column was the email as I had also been passing it with a hardcoded email to save me time from inserting data into the textfields.
Not only that before the database error I was halted by a CSRF token error as I was not getting any response at all. It would be better to set up a standard POST form first before using ajax as I’d be able to clearly identify a number of things -
That the URI is correct
The CSRF token has been passed
The error response is correctly formatted and easily readable
The success response is correctly formatted and accessible
Finally upon reaching a successful response but failure was in regards to point 4. The success is that the code on the server was correct - the object was created and saved to the database. Each time I’d reload the page and enter in my details it would create a new row. But the response wasn’t formatted correctly. I should remind myself that using the response()->json(); passes a JSON object to the client side and there is no need to JSON.stringify() or JSON.parse() the response. Though JSON.stringify() is useful to print out the response and JSON.parse() is useful to convert a string that is currently using JSON syntax to be converted to a JSON Object. Laravel does convert my model/class to JSON for me however with the response()->json() method.