Skip to content
Advertisement

Lumen: The new record with default values was added to database

I’m new to PHP and I’ve tried to make the small test full-stack project. I have Vue.js app on the client (frontend) and PHP (Lumen) on the server (backend). The code looks as following:

Client:

Vue component:

JavaScript

Server:

Router:

JavaScript

Model:

JavaScript

Controller:

JavaScript

Database:

enter image description here

enter image description here

Debug session on server side – $request value:

enter image description here

The issue is the new record was added to database, but with default values that I set on database level. I’m not sure why the object I’ve passed on the client is not added.

JavaScript

And the last thing – it works if I use Postman. But, as you can see, it doesn’t work with Axios.

Advertisement

Answer

Your problem is that you are changing what the content type of your request is. Do not write headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, as axios sends this as 'Content-Type': 'application/json', so when it arrives to Lumen, it “decodes” it correctly and you can do $request->all() and get any data you want. You don’t even have to write any content-type header, it is all automatically done by axios in this case.

So your javascript code should be like:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement